Keras-Sequential模型(2)

来源:互联网 发布:腾讯软件 编辑:程序博客网 时间:2024/06/14 23:43

重点介绍Sequential模型方法

compile

compile(self, optimizer, loss, metrics=[], sample_weight_mode=None, **kwargs)

编译用来配置模型的学习过程,其参数有:

  • 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(32, input_shape=(500,)))model.add(Dense(10, activation='softmax'))model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])

fit

fit(self, x, y, batch_size=32, nb_epoch=10, verbose=1, callbacks=[], validation_split=0.0, 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=32, verbose=1, 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中各个值的含义。

0 0
原创粉丝点击