天天看點

Keras-Sequential模型(2)

重點介紹Sequential模型方法

compile

編譯用來配置模型的學習過程,其參數有:
  • optimizer: str (name of optimizer) or optimizer object.
    optimizer:字元串(預定義優化器名)或優化器對象,參考優化器
  • loss: str (name of objective function) or objective function.
    loss:字元串(預定義損失函數名)或目标函數
  • metrics: list of metrics to be evaluated by the model.during training and testing.Typically you will use “metrics=[‘accuracy’] “.
    metrics:清單,包含評估模型在訓練和測試時的網絡性能的名額,典型用法是metrics=[‘accuracy’]
  • sample_weight_mode: if you need to do timestep-wise.sample weighting (2D weights), set this to “temporal”.”None” defaults to sample-wise weights (1D).
    sample_weight_mode:如果你需要按時間步為樣本賦權(2D權矩陣),将該值設為“temporal”。預設為“None”,代表按樣本賦權(1D權)
  • kwargs: for Theano backend, these are passed into K.function.
    kwargs:使用TensorFlow作為後端請忽略該參數,若使用Theano作為後端,kwargs的值将會傳遞給 K.function
  • example:
    model = Sequential()
    model.add(Dense(, input_shape=(,)))
    model.add(Dense(, activation='softmax'))
    model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
               

fit

fit(self, x, y, batch_size=, nb_epoch=, verbose=, callbacks=[], validation_split=, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, **kwargs)
           
本函數将模型訓練nb_epoch輪,其參數有:
  • x: input data, as a Numpy array or list of Numpy arrays (if the model has multiple inputs).
    x:輸入資料。如果模型隻有一個輸入,那麼x的類型是numpy array,如果模型有多個輸入,那麼x的類型應當為list,list的元素是對應于各個輸入的numpy array
  • y: labels, as a Numpy array.
    y:标簽,numpy array
  • batch_size: integer. Number of samples per gradient update.
    batch_size:整數,指定進行梯度下降時每個batch包含的樣本數。訓練時一個batch的樣本會被計算一次梯度下降,使目标函數優化一步。
  • nb_epoch: integer, the number of epochs to train the model.
    nb_epoch:整數,訓練的輪數,訓練資料将會被周遊nb_epoch次。Keras中nb開頭的變量均為”number of”的意思
  • verbose: 0 for no logging to stdout,1 for progress bar logging, 2 for one log line per epoch.
    verbose:日志顯示,0為不在标準輸出流輸出日志資訊,1為輸出進度條記錄,2為每個epoch輸出一行記錄。
  • callbacks: list of ‘keras.callbacks.Callback’instances.List of callbacks to apply during training.
    callbacks:list,其中的元素是keras.callbacks.Callback的對象。這個list中的回調函數将會在訓練過程中的适當時機被調用,參考回調函數.
  • validation_split: float (0. < x < 1).Fraction of the data to use as held-out validation data.
    validation_split:0~1之間的浮點數,用來指定訓練集的一定比例資料作為驗證集。驗證集将不參與訓練,并在每個epoch結束後測試的模型的名額,如損失函數、精确度等。
  • validation_data: tuple (x_val, y_val) or tuple (x_val, y_val, val_sample_weights) to be used as held-out validation data. Will override validation_split.
    validation_data:形式為(X,y)的tuple,是指定的驗證集。此參數将覆寫validation_spilt。
  • shuffle: boolean or str (for ‘batch’).Whether to shuffle the samples at each epoch.’batch’ is a special option for dealing with the limitations of HDF5 data; it shuffles in batch-sized chunks
    shuffle:布爾值或字元串,一般為布爾值,表示是否在訓練過程中随機打亂輸入樣本的順序。若為字元串“batch”,則是用來處理HDF5資料的特殊情況,它将在batch内部将資料打亂。
  • class_weight: dictionary mapping classes to a weight value, used for scaling the loss function (during training only).
    class_weight:字典,将不同的類别映射為不同的權值,該參數用來在訓練過程中調整損失函數(隻能用于訓練)
  • sample_weight: Numpy array of weights for the training samples, used for scaling the loss function(during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode=”temporal” in compile().
    sample_weight:權值的numpy array,用于在訓練時調整損失函數(僅用于訓練)。可以傳遞一個1D的與樣本等長的向量用于對樣本進行1對1的權重,或者在面對時序資料時,傳遞一個的形式為(samples,sequence_length)的矩陣來為每個時間步上的樣本賦不同的權。這種情況下請确定在編譯模型時添加了sample_weight_mode=’temporal’。

evaluate

evaluate(self, x, y, batch_size=, verbose=, sample_weight=None, **kwargs)
           
本函數按batch計算在某些輸入資料上模型的誤差,其參數有:
  • x: input data, as a Numpy array or list of Numpy arrays (if the model has multiple inputs).
    x:輸入資料,與fit一樣,是numpy array或numpy array的list
  • y: labels, as a Numpy array.
    y:标簽,numpy array
  • batch_size: integer. Number of samples per gradient update.
    batch_size:整數,含義同fit的同名參數
  • verbose: verbosity mode, 0 or 1.
    verbose:含義同fit的同名參數,但隻能取0或1
  • sample_weight: sample weights, as a Numpy array.
    sample_weight:numpy array,含義同fit的同名參數

本函數傳回一個測試誤差的标量值(如果模型沒有其他評價名額),或一個标量的list(如果模型還有其他的評價名額)。

model.metrics_names将給出list中各個值的含義。

繼續閱讀