美文网首页
keras入门(二)

keras入门(二)

作者: dario0558 | 来源:发表于2020-01-29 21:15 被阅读0次

    About keras layer

    introduction: https://keras.io/layers/about-keras-layers/

    All Keras layers have a number of methods in common:

    layer.get_weights(): returns the weights of the layer as a list of Numpy arrays.
    layer.set_weights(weights): sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of get_weights).
    layer.get_config(): returns a dictionary containing the configuration of the layer. The layer can be reinstantiated from its config via:
    layer = Dense(32)
    config = layer.get_config()
    reconstructed_layer = Dense.from_config(config)
    Or:

    from keras import layers

    config = layer.get_config()
    layer = layers.deserialize({'class_name': layer.class.name,
    'config': config})
    If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via:

    layer.input
    layer.output
    layer.input_shape
    layer.output_shape
    If the layer has multiple nodes (see: the concept of layer node and shared layers), you can use the following methods:

    layer.get_input_at(node_index)
    layer.get_output_at(node_index)
    layer.get_input_shape_at(node_index)
    layer.get_output_shape_at(node_index)

    About Keras model -- Sequential methods

    keras典型框架
    主要讲述了keras经典模型--Sequential模型中model的顺序创建连接方法。
    另外keras训练基本套路,compile, fit, evaluate, predict.
    指定模型Sequential( )
    ---->堆叠模块 .add( )
    ---->编译模型 .compile( )
    ---->在训练数据上迭代 .fit( )
    ---->评估 .evaluate( )
    ---->对新数据的预测 .predict( )

    Sequential模型的methods
    https://keras.io/models/sequential/

    (1)compile
    compile(optimizer, loss=None, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)

    description: Configures the model for training.

    (2)fit

    fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0,
    validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0,
    steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)

    description: Trains the model for a fixed number of epochs (iterations on a dataset).

    Arguments:
    x: Input data.
    y: Target data.
    batch_size: Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32
    epochs:整数,训练终止时的epoch值,训练将在达到该epoch值时停止,当没有设置initial_epoch时,它就是训练的总轮数,否则训练的总轮数为epochs - inital_epoch
    verbose:日志显示,0为不在标准输出流输出日志信息,1为输出进度条记录,2为每个epoch输出一行记录
    validation_split:0~1之间的浮点数,用来指定训练集的一定比例数据作为验证集。验证集将不参与训练,并在每个epoch结束后测试的模型的指标,如损失函数、精确度等。
    注意,validation_split的划分在shuffle之前,因此如果你的数据本身是有序的,需要先手工打乱再指定validation_split,否则可能会出现验证集样本不均匀。
    validation_data:形式为(X,y)的tuple,是指定的验证集。此参数将覆盖validation_spilt。
    shuffle:布尔值或字符串,一般为布尔值,表示是否在训练过程中随机打乱输入样本的顺序。若为字符串“batch”,则是用来处理HDF5数据的特殊情况,它将在batch内部将数据打乱。

    Reference:
    https://blog.csdn.net/LuYi_WeiLin/article/details/88555813

    (3)evaluate

    evaluate(x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)

    description: Returns the loss value & metrics values for the model in test mode. Computation is done in batches.

    Arguments:
    x: Input data.
    y: Target data.
    batch_size: Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32

    Reference:

    (4)predict
    predict(x, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)

    description: Generates output predictions for the input samples

    Returns

    Numpy array(s) of predictions.

    (二)batch-xxx

    (1)train_on_batch
    train_on_batch(x, y, sample_weight=None, class_weight=None, reset_metrics=True)

    description:
    Arguments:
    Reference:

    About keras model

    common methods and attributes:

    model.layers -- a flattended list of the layers comprising the model
    model.get_layer(self,name=None,index=None):依据层名或下标获得层对象
    model.inputs -- the list of input tensors of the model
    model.outputs -- the list of output tensors of the model

    model.summary() -- a summary representation of your model. Shortcut for utils.print_summary

    model.summary()


    Layer (type) Output Shape Param #

    dense_4 (Dense) (None, 7) 35


    activation_4 (Activation) (None, 7) 0


    dense_5 (Dense) (None, 13) 104


    activation_5 (Activation) (None, 13) 0


    dense_6 (Dense) (None, 5) 70


    activation_6 (Activation) (None, 5) 0

    Total params: 209
    Trainable params: 209
    Non-trainable params: 0


    知识点:

    1. 详解keras的model.summary()输出参数Param计算过程
      https://blog.csdn.net/ybdesire/article/details/85217688

    model.get_config() -- returns a dict containing the configuration of the model.

    model.get_weights() -- a list of all weight tensors in the model,as numpy arrays.
    model.set_weights(weights) -- sets the values of the weights of the model, from a list of Numpy arrays.
    The arrays in the list should have the same shape as those returned by get_weights()

    model.to_json() returns a representation of the model as a JSON string. Note that the representation does not include the weights,
    only the architecture. You can reinstantiate the same model (with reinitialized weights) from the JSON string via:

    from keras.models import model_from_json

    json_string = model.to_json()
    model = model_from_json(json_string)

    相关文章

      网友评论

          本文标题:keras入门(二)

          本文链接:https://www.haomeiwen.com/subject/lmebthtx.html