zoukankan      html  css  js  c++  java
  • 深度学习(六)keras常用函数学习

    原文作者:aircraft

    原文链接:https://www.cnblogs.com/DOMLX/p/9769301.html

    深度学习教程目录如下,还在继续更新完善中

    深度学习系列教程目录

     Keras是什么?

    Keras:基于Theano和TensorFlow的深度学习库
    Keras是一个高层神经网络API,Keras由纯Python编写而成并基Tensorflow、Theano以及CNTK后端。Keras 为支持快速实验而生,能够把你的idea迅速转换为结果,如果你有如下需求,请选择Keras:
    • 简易和快速的原型设计(keras具有高度模块化,极简,和可扩充特性)
    • 支持CNN和RNN,或二者的结合
    • 无缝CPU和GPU切换

    如果还没有配置keras可以这个博客配置:

    2018最新win10 安装tensorflow1.4(GPU/CPU)+cuda8.0+cudnn8.0-v6 + keras 安装CUDA失败 导入tensorflow失败报错问题解决

     

    kears Dense()函数--全连接层

    keras.layers.core.Dense ( units, activation=None, 
                   use_bias=True, 
                   kernel_initializer='glorot_uniform'
                   bias_initializer='zeros'
                   kernel_regularizer=None, 
                   bias_regularizer=None, 
                   activity_regularizer=None, 
                   kernel_constraint=None, 
                   bias_constraint=None  )
    参数
    units:大于0的整数,代表该层的输出维度。
    activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)
    use_bias: 布尔值,是否使用偏置项
    kernel_initializer:权值初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers
    bias_initializer:权值初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers
    kernel_regularizer:施加在权重上的正则项,为Regularizer对象
    bias_regularizer:施加在偏置向量上的正则项,为Regularizer对象
    activity_regularizer:施加在输出上的正则项,为Regularizer对象
    kernel_constraints:施加在权重上的约束项,为Constraints对象
    bias_constraints:施加在偏置上的约束项,为Constraints对象
    input_dim:可以指定输入数据的维度

    kears Conv2D()函数--卷积层

    若不懂卷积概念可看:深度学习(二)神经网络中的卷积和反卷积原理

     

    keras.layers.Conv2D(filters, kernel_size, 
              strides=(1, 1),
              padding='valid',
              data_format=None,
              dilation_rate=(1, 1),
              activation=None, use_bias=True,
              kernel_initializer='glorot_uniform',
              bias_initializer='zeros',
              kernel_regularizer=None,
              bias_regularizer=None,
              activity_regularizer=None,
              kernel_constraint=None,
              bias_constraint=None)

    2D 卷积层 (例如对图像的空间卷积)。

    该层创建了一个卷积核, 该卷积核对层输入进行卷积, 以生成输出张量。 如果 use_bias 为 True, 则会创建一个偏置向量并将其添加到输出中。 最后,如果 activation 不是 None,它也会应用于输出。

    当使用该层作为模型第一层时,需要提供 input_shape 参数 (整数元组,不包含样本表示的轴),例如, input_shape=(128, 128, 3) 表示 128x128 RGB 图像, 在 data_format="channels_last" 时。

    参数

    • filters: 整数,输出空间的维度 (即卷积中滤波器的输出数量)。
    • kernel_size: 一个整数,或者 2 个整数表示的元组或列表, 指明 2D 卷积窗口的宽度和高度。 可以是一个整数,为所有空间维度指定相同的值。
    • strides: 一个整数,或者 2 个整数表示的元组或列表, 指明卷积沿宽度和高度方向的步长。 可以是一个整数,为所有空间维度指定相同的值。 指定任何 stride 值 != 1 与指定 dilation_rate 值 != 1 两者不兼容。
    • padding: "valid""same" (大小写敏感)。
    • data_format: 字符串, channels_last (默认) 或 channels_first 之一,表示输入中维度的顺序。 channels_last 对应输入尺寸为 (batch, height, width, channels)channels_first 对应输入尺寸为 (batch, channels, height, width)。 它默认为从 Keras 配置文件 ~/.keras/keras.json 中 找到的 image_data_format 值。 如果你从未设置它,将使用 "channels_last"。
    • dilation_rate: 一个整数或 2 个整数的元组或列表, 指定膨胀卷积的膨胀率。 可以是一个整数,为所有空间维度指定相同的值。 当前,指定任何 dilation_rate 值 != 1 与 指定 stride 值 != 1 两者不兼容。
    • activation: 要使用的激活函数 (详见 activations)。 如果你不指定,则不使用激活函数 (即线性激活: a(x) = x)。
    • use_bias: 布尔值,该层是否使用偏置向量。
    • kernel_initializer: kernel 权值矩阵的初始化器 (详见 initializers)。
    • bias_initializer: 偏置向量的初始化器 (详见 initializers)。
    • kernel_regularizer: 运用到 kernel 权值矩阵的正则化函数 (详见 regularizer)。
    • bias_regularizer: 运用到偏置向量的正则化函数 (详见 regularizer)。
    • activity_regularizer: 运用到层输出(它的激活值)的正则化函数 (详见 regularizer)。
    • kernel_constraint: 运用到 kernel 权值矩阵的约束函数 (详见 constraints)。
    • bias_constraint: 运用到偏置向量的约束函数 (详见 constraints)。

    输入尺寸

    • 如果 data_format='channels_first', 输入 4D 张量,尺寸为 (samples, channels, rows, cols)
    • 如果 data_format='channels_last', 输入 4D 张量,尺寸为 (samples, rows, cols, channels)

    输出尺寸

    • 如果 data_format='channels_first', 输出 4D 张量,尺寸为 (samples, filters, new_rows, new_cols)
    • 如果 data_format='channels_last', 输出 4D 张量,尺寸为 (samples, new_rows, new_cols, filters)

    别看上面的参数一堆吓死人,其实我们在实际运用的时候用的就只有几个而已:

    inputs = Input(shape=(n_ch,patch_height,patch_width))
    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same',data_format='channels_first')(inputs)  #这个小括号填inputs是代表这层模型连接在inputs之后

    当然还可以用kears内置的序贯模型add添加构成模型图:

    model = Sequential()
    # Dense(64) is a fully-connected layer with 64 hidden units.
    # in the first layer, you must specify the expected input data shape:
    # here, 20-dimensional vectors.
    model.add(Dense(64, activation='relu', input_dim=20))

    kears MaxPooling2D()函数--池化层

    若不懂池化概念可看:深度学习(一)神经网络中的池化与反池化原理

     

    keras.layers.pooling.MaxPooling2D( pool_size=(2, 2), strides=None, padding='valid', data_format=None )
    参数:
    pool_size:整数或长为2的整数tuple,代表在两个方向(竖直,水平)上的下采样因子,如取(2,2)将使图片在两个维度上均变为原长的一半。为整数意为各个维度值相同且为该数字。
    strides:整数或长为2的整数tuple,或者None,步长值。
    padding:‘valid’或者‘same’
    data_format:字符串,“channels_first”或“channels_last”之一,代表图像的通道维的位置。该参数是Keras 1.x中的image_dim_ordering,“channels_last”对应原本的“tf”,“channels_first”对应原本的“th”。以128x128的RGB图像为例,“channels_first”应将数据组织为(3,128,128),而“channels_last”应将数据组织为(128,128,3)。该参数的默认值是~/.keras/keras.json中设置的值,若从未设置过,则为“channels_last”。

    还是一样的好多东西默认就行了,下面就是一个2*2的池化层:

    pool1 = MaxPooling2D((2, 2))(conv1)

    kears  model.compile()函数--配置模型

    model.compile(optimizer, loss, metrics=None, sample_weight_mode=None)

    编译用来配置模型的学习过程,其参数有
    optimizer:字符串(预定义优化器名)或优化器对象,参考优化器 
    loss:字符串(预定义损失函数名)或目标函数,参考损失函数
    metrics:列表,包含评估模型在训练和测试时的网络性能的指标,典型用法是metrics=['accuracy']
    sample_weight_mode:如果你需要按时间步为样本赋权(2D权矩阵),将该值设为“temporal”。默认为“None”,代表按样本赋权(1D权)。在下面fit函数的解释中有相关的参考内容。
    kwargs:使用TensorFlow作为后端请忽略该参数,若使用Theano作为后端,kwargs的值将会传递给 K.function

    示例代码:

    model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])

    kears  model.fit()函数--模型运行函数

    fit(self, x, y, batch_size=32, epochs=10, verbose=1, callbacks=None, validation_split=0.0,
      validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0 )

    x:输入数据。如果模型只有一个输入,那么x的类型是numpy array,如果模型有多个输入,那么x的类型应当为list,list的元素是对应于各个输入的numpy array

    y:标签,numpy array
    batch_size:整数,指定进行梯度下降时每个batch包含的样本数。训练时一个batch的样本会被计算一次梯度下降,使目标函数优化一步。
    epochs:整数,训练的轮数,每个epoch会把训练集轮一遍。
    verbose:日志显示,0为不在标准输出流输出日志信息,1为输出进度条记录,2为每个epoch输出一行记录
    callbacks:list,其中的元素是keras.callbacks.Callback的对象。这个list中的回调函数将会在训练过程中的适当时机被调用,参考回调函数
    validation_split:0~1之间的浮点数,用来指定训练集的一定比例数据作为验证集。验证集将不参与训练,并在每个epoch结束后测试的模型的指标,如损失函数、精确度等。注意,validation_split的划分在shuffle之前,因此如果你的数据本身是有序的,需要先手工打乱再指定validation_split,否则可能会出现验证集样本不均匀。
    validation_data:形式为(X,y)的tuple,是指定的验证集。此参数将覆盖validation_spilt。
    shuffle:布尔值或字符串,一般为布尔值,表示是否在训练过程中随机打乱输入样本的顺序。若为字符串“batch”,则是用来处理HDF5数据的特殊情况,它将在batch内部将数据打乱。
    class_weight:字典,将不同的类别映射为不同的权值,该参数用来在训练过程中调整损失函数(只能用于训练)

    sample_weight:权值的numpy array,用于在训练时调整损失函数(仅用于训练)。可以传递一个1D的与样本等长的向量用于对样本进行1对1的加权,或者在面对时序数据时,传递一个的形式为(samples,sequence_length)的矩阵来为每个时间步上的样本赋不同的权。这种情况下请确定在编译模型时添加了sample_weight_mode='temporal'。

    initial_epoch: 从该参数指定的epoch开始训练,在继续之前的训练时有用。

    参数虽多,但是很多都可以省略看代码示例:

    model.fit(patches_imgs_train, patches_masks_train, epochs=N_epochs, batch_size=batch_size, verbose=1, shuffle=True, validation_split=0.1, callbacks=[checkpointer])

    kears  predict()函数--测试数据

    predictions = model.predict(patches_imgs_test, batch_size=32, verbose=2)
    print("predicted images size :")
    print(predictions.shape)

    kears  load_weights()函数--直接导入训练好的模型

    # 加载训练好的模型
    model.load_weights('./weights.h5')

    kears  Dropout()函数--抛弃一些参数防止过拟合

    Dropout(x)
    X可以取0--1之间,代表百分比抛弃数据
    Dropout(0.5)随机抛弃百分之五十的数据

    kears UpSampling2D()函数--上采样函数

    UpSampling2D(size=(2, 2))

    size(x,y)

    x代表行放大倍数  这里取2的话代表原来的一行变成了两行 (就是一行那么粗,变成了两行那么粗)

    y代表列放大倍数  这里取2的话代表原来的一变成了两行 (就是一那么粗,变成了两那么粗)

    size(2,2)其实就等于将原图放大四倍(水平两倍,垂直两倍) 32*32 变成 62*64的图像

    kears Model()函数--代表模型图

    inputs = Input((n_ch, patch_height, patch_width))
        conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs)
        conv1 = Dropout(0.2)(conv1)
        conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv1)
        up1 = UpSampling2D(size=(2, 2))(conv1)
        #
        conv2 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(up1)
        conv2 = Dropout(0.2)(conv2)
        conv2 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(conv2)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv2)
        #
        conv3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(pool1)
        conv3 = Dropout(0.2)(conv3)
        conv3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv3)
        pool2 = MaxPooling2D(pool_size=(2, 2))(conv3)
        #
        conv4 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(pool2)
        conv4 = Dropout(0.2)(conv4)
        conv4 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv4)
        pool3 = MaxPooling2D(pool_size=(2, 2))(conv4)
        #
        conv5 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(pool3)
        conv5 = Dropout(0.2)(conv5)
        conv5 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(conv5)
        #
        up2 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
        conv6 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(up2)
        conv6 = Dropout(0.2)(conv6)
        conv6 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv6)
        #
        up3 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1)
        conv7 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(up3)
        conv7 = Dropout(0.2)(conv7)
        conv7 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv7)
        #
        up4 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1)
        conv8 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(up4)
        conv8 = Dropout(0.2)(conv8)
        conv8 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(conv8)
        #
        pool4 = MaxPooling2D(pool_size=(2, 2))(conv8)
        conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(pool4)
        conv9 = Dropout(0.2)(conv9)
        conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv9)
        #
        conv10 = Convolution2D(2, 1, 1, activation='relu', border_mode='same')(conv9)
        conv10 = core.Reshape((2,patch_height*patch_width))(conv10)
        conv10 = core.Permute((2,1))(conv10)
        ############
        conv10 = core.Activation('softmax')(conv10)
    
        model = Model(input=inputs, output=conv10)

    将模型的输入和输出给model函数就会自己组建模型运行图结构

    kears Embedding()函数--嵌入层

    keras.layers.embeddings.Embedding( input_dim, output_dim, embeddings_initializer='uniform', 
                        embeddings_regularizer=None, activity_regularizer=None, embeddings_constraint=None, mask_zero=False, input_length=None)
    作用:嵌入层将正整数(下标)转换为具有固定大小的向量,如[[4],[20]]->[[0.25,0.1],[0.6,-0.2]]。Embedding层只能作为模型的第一层。
    input_dim:大或等于0的整数,字典长度,即输入数据最大下标+1,就是矩阵中的最大值
    output_dim:大于0的整数,代表全连接嵌入的维度
    embeddings_initializer: 嵌入矩阵的初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers
    embeddings_regularizer: 嵌入矩阵的正则项,为Regularizer对象
    embeddings_constraint: 嵌入矩阵的约束项,为Constraints对象
    mask_zero:布尔值,确定是否将输入中的‘0’看作是应该被忽略的‘填充’(padding)值,该参数在使用递归层处理变长输入时有用。设置为True的话,模型中后续的层必须都支持masking,否则会抛出异常。如果该值为True,则下标0在字典中不可用,input_dim应设置为|vocabulary| + 2。
    input_length:当输入序列的长度固定时,该值为其长度。如果要在该层后接Flatten层,然后接Dense层,则必须指定该参数,否则Dense层的输出维度无法自动推断。

    关于embeding作用的详细介绍:http://spaces.ac.cn/archives/4122/

    kears normalization()函数--标准化

    keras.layers.normalization.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, 
    scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros',
    moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None,
    beta_constraint=None, gamma_constraint=None)

    该层在每个batch上将前一层的激活值重新规范化,即使得其输出数据的均值接近0,其标准差接近1

    参数

    • axis: 整数,指定要规范化的轴,通常为特征轴。例如在进行data_format="channels_first的2D卷积后,一般会设axis=1。
    • momentum: 动态均值的动量
    • epsilon:大于0的小浮点数,用于防止除0错误
    • center: 若设为True,将会将beta作为偏置加上去,否则忽略参数beta
    • scale: 若设为True,则会乘以gamma,否则不使用gamma。当下一层是线性的时,可以设False,因为scaling的操作将被下一层执行。
    • beta_initializer:beta权重的初始方法
    • gamma_initializer: gamma的初始化方法
    • moving_mean_initializer: 动态均值的初始化方法
    • moving_variance_initializer: 动态方差的初始化方法
    • beta_regularizer: 可选的beta正则
    • gamma_regularizer: 可选的gamma正则
    • beta_constraint: 可选的beta约束
    • gamma_constraint: 可选的gamma约束

    输入shape

    任意,当使用本层为模型首层时,指定input_shape参数时有意义。

    输出shape

    与输入shape相同

    kears plot()函数--画出模型图

    plot(model, to_file='./'+name_experiment+'/'+name_experiment + '_model.png')

    kears中可以将自己建立的模型图画出来,传进去一个模型,指定画出文件的路径和名字即可

    kears ModelCheckpoint()函数--保存模型参数

    checkpointer = ModelCheckpoint(filepath='./'+name_experiment+'/'+name_experiment +'_best_weights.h5', verbose=1, monitor='val_loss', mode='auto', save_best_only=True)
    
    model.fit(patches_imgs_train, patches_masks_train, epochs=N_epochs, batch_size=batch_size, verbose=1, shuffle=True, validation_split=0.1, callbacks=[checkpointer])
    ModelCheckpoint函数可以指定一定训练次数后保存中间训练的最佳参数
    ModelCheckpoint函数作为model.fit()函数中回调函数使用

    kears merge()函数--融合层

    Merge层提供了一系列用于融合两个层或两个张量的层对象和方法。以大写首字母开头的是Layer类,以小写字母开头的是张量的函数。小写字母开头的张量函数在内部实际上是调用了大写字母开头的层。

    keras.layers.Add()用法

    keras.layers.Add()
    

    添加输入列表的图层。

    该层接收一个相同shape列表张量,并返回它们的和,shape不变。

    Example

    import keras
    
    input1 = keras.layers.Input(shape=(16,))
    x1 = keras.layers.Dense(8, activation='relu')(input1)
    input2 = keras.layers.Input(shape=(32,))
    x2 = keras.layers.Dense(8, activation='relu')(input2)
    added = keras.layers.Add()([x1, x2])  # equivalent to added = keras.layers.add([x1, x2])
    
    out = keras.layers.Dense(4)(added)
    model = keras.models.Model(inputs=[input1, input2], outputs=out)
    

    keras.layers.Subtract()用法

    keras.layers.Subtract()
    

    两个输入的层相减。

    它将大小至少为2,相同Shape的列表张量作为输入,并返回一个张量(输入[0] - 输入[1]),也是相同的Shape。

    Example

    import keras
    
    input1 = keras.layers.Input(shape=(16,))
    x1 = keras.layers.Dense(8, activation='relu')(input1)
    input2 = keras.layers.Input(shape=(32,))
    x2 = keras.layers.Dense(8, activation='relu')(input2)
    # Equivalent to subtracted = keras.layers.subtract([x1, x2])
    subtracted = keras.layers.Subtract()([x1, x2])
    
    out = keras.layers.Dense(4)(subtracted)
    model = keras.models.Model(inputs=[input1, input2], outputs=out)
    

    keras.layers.Multiply()用法

    keras.layers.Multiply()
    

    该层接收一个列表的同shape张量,并返回它们的逐元素积的张量,shape不变。

    keras.layers.Average()用法

    keras.layers.Average()
    

    该层接收一个列表的同shape张量,并返回它们的逐元素均值,shape不变。

    keras.layers.Maximum()用法

    keras.layers.Maximum()
    

    该层接收一个列表的同shape张量,并返回它们的逐元素最大值,shape不变。

    keras.layers.Concatenate(axis=-1)参数

    keras.layers.Concatenate(axis=-1)
    

    该层接收一个列表的同shape张量,并返回它们的按照给定轴相接构成的向量。

    参数

    • axis: 想接的轴
    • **kwargs: 普通的Layer关键字参数

    keras.layers.Dot(axes, normalize=False)参数

    keras.layers.Dot(axes, normalize=False)
    

    计算两个tensor中样本的张量乘积。例如,如果两个张量ab的shape都为(batch_size, n),则输出为形如(batch_size,1)的张量,结果张量每个batch的数据都是a[i,:]和b[i,:]的矩阵(向量)点积。

    参数

    • axes: 整数或整数的tuple,执行乘法的轴。
    • normalize: 布尔值,是否沿执行成绩的轴做L2规范化,如果设为True,那么乘积的输出是两个样本的余弦相似性。
    • **kwargs: 普通的Layer关键字参数

    keras.layers.add(inputs)参数

    keras.layers.add(inputs)
    

    Add层的函数式包装

    参数:

    • inputs: 长度至少为2的张量列表A
    • **kwargs: 普通的Layer关键字参数

    返回值

    输入列表张量之和

    Example

    import keras
    
    input1 = keras.layers.Input(shape=(16,))
    x1 = keras.layers.Dense(8, activation='relu')(input1)
    input2 = keras.layers.Input(shape=(32,))
    x2 = keras.layers.Dense(8, activation='relu')(input2)
    added = keras.layers.add([x1, x2])
    
    out = keras.layers.Dense(4)(added)
    model = keras.models.Model(inputs=[input1, input2], outputs=out)
    

    keras.layers.subtract(inputs)参数

    keras.layers.subtract(inputs)
    

    Subtract层的函数式包装

    参数:

    • inputs: 长度至少为2的张量列表A
    • **kwargs: 普通的Layer关键字参数

    返回值

    输入张量列表的差别

    Example

    import keras
    
    input1 = keras.layers.Input(shape=(16,))
    x1 = keras.layers.Dense(8, activation='relu')(input1)
    input2 = keras.layers.Input(shape=(32,))
    x2 = keras.layers.Dense(8, activation='relu')(input2)
    subtracted = keras.layers.subtract([x1, x2])
    
    out = keras.layers.Dense(4)(subtracted)
    model = keras.models.Model(inputs=[input1, input2], outputs=out)
    

    keras.layers.multiply(inputs)参数

    keras.layers.multiply(inputs)
    

    Multiply的函数式包装

    参数:

    • inputs: 长度至少为2的张量列表
    • **kwargs: 普通的Layer关键字参数

    返回值

    输入列表张量之逐元素积

    keras.layers.average(inputs)参数

    keras.layers.average(inputs)
    

    Average的函数包装

    参数:

    • inputs: 长度至少为2的张量列表
    • **kwargs: 普通的Layer关键字参数

    返回值

    输入列表张量之逐元素均值

    keras.layers.maximum(inputs)参数

    keras.layers.maximum(inputs)
    

    Maximum的函数包装

    参数:

    • inputs: 长度至少为2的张量列表
    • **kwargs: 普通的Layer关键字参数

    返回值

    输入列表张量之逐元素均值

    keras.layers.concatenate(inputs, axis=-1)参数

    keras.layers.concatenate(inputs, axis=-1)
    

    Concatenate的函数包装

    参数

    • inputs: 长度至少为2的张量列
    • axis: 相接的轴
    • **kwargs: 普通的Layer关键字参数

    keras.layers.dot(inputs, axes, normalize=False)参数

    keras.layers.dot(inputs, axes, normalize=False)
    

    Dot的函数包装

    参数

    • inputs: 长度至少为2的张量列
    • axes: 整数或整数的tuple,执行乘法的轴。
    • normalize: 布尔值,是否沿执行成绩的轴做L2规范化,如果设为True,那么乘积的输出是两个样本的余弦相似性。
    • **kwargs: 普通的Layer关键字参数

    kears core()模块函数--常用层

    Activation层

    keras.layers.core.Activation(activation)
    

    激活层对一个层的输出施加激活函数

    参数

    • activation:将要使用的激活函数,为预定义激活函数名或一个Tensorflow/Theano的函数。参考激活函数

    输入shape

    任意,当使用激活层作为第一层时,要指定input_shape

    输出shape

    与输入shape相同


     

    Dropout层

    keras.layers.core.Dropout(rate, noise_shape=None, seed=None)
    

    为输入数据施加Dropout。Dropout将在训练过程中每次更新参数时按一定概率(rate)随机断开输入神经元,Dropout层用于防止过拟合。

    参数

    • rate:0~1的浮点数,控制需要断开的神经元的比例

    • noise_shape:整数张量,为将要应用在输入上的二值Dropout mask的shape,例如你的输入为(batch_size, timesteps, features),并且你希望在各个时间步上的Dropout mask都相同,则可传入noise_shape=(batch_size, 1, features)。

    • seed:整数,使用的随机数种子

    参考文献


    Flatten层

    keras.layers.core.Flatten()
    

    Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小。

    例子

    model = Sequential()
    model.add(Convolution2D(64, 3, 3,
                border_mode='same',
                input_shape=(3, 32, 32)))
    # now: model.output_shape == (None, 64, 32, 32)
    
    model.add(Flatten())
    # now: model.output_shape == (None, 65536)
    

    Reshape层

    keras.layers.core.Reshape(target_shape)
    

    Reshape层用来将输入shape转换为特定的shape

    参数

    • target_shape:目标shape,为整数的tuple,不包含样本数目的维度(batch大小)

    输入shape

    任意,但输入的shape必须固定。当使用该层为模型首层时,需要指定input_shape参数

    输出shape

    (batch_size,)+target_shape

    例子

    # as first layer in a Sequential model
    model = Sequential()
    model.add(Reshape((3, 4), input_shape=(12,)))
    # now: model.output_shape == (None, 3, 4)
    # note: `None` is the batch dimension
    
    # as intermediate layer in a Sequential model
    model.add(Reshape((6, 2)))
    # now: model.output_shape == (None, 6, 2)
    
    # also supports shape inference using `-1` as dimension
    model.add(Reshape((-1, 2, 2)))
    # now: model.output_shape == (None, 3, 2, 2)
    

    Permute层

    keras.layers.core.Permute(dims)
    

    Permute层将输入的维度按照给定模式进行重排,例如,当需要将RNN和CNN网络连接时,可能会用到该层。

    参数

    • dims:整数tuple,指定重排的模式,不包含样本数的维度。重拍模式的下标从1开始。例如(2,1)代表将输入的第二个维度重拍到输出的第一个维度,而将输入的第一个维度重排到第二个维度

    例子

    model = Sequential()
    model.add(Permute((2, 1), input_shape=(10, 64)))
    # now: model.output_shape == (None, 64, 10)
    # note: `None` is the batch dimension
    

    输入shape

    任意,当使用激活层作为第一层时,要指定input_shape

    输出shape

    与输入相同,但是其维度按照指定的模式重新排列


    RepeatVector层

    keras.layers.core.RepeatVector(n)
    

    RepeatVector层将输入重复n次

    参数

    • n:整数,重复的次数

    输入shape

    形如(nb_samples, features)的2D张量

    输出shape

    形如(nb_samples, n, features)的3D张量

    例子

    model = Sequential()
    model.add(Dense(32, input_dim=32))
    # now: model.output_shape == (None, 32)
    # note: `None` is the batch dimension
    
    model.add(RepeatVector(3))
    # now: model.output_shape == (None, 3, 32)
    
    

    Lambda层

    keras.layers.core.Lambda(function, output_shape=None, mask=None, arguments=None)
    

    本函数用以对上一层的输出施以任何Theano/TensorFlow表达式

    参数

    • function:要实现的函数,该函数仅接受一个变量,即上一层的输出

    • output_shape:函数应该返回的值的shape,可以是一个tuple,也可以是一个根据输入shape计算输出shape的函数

    • mask: 掩膜

    • arguments:可选,字典,用来记录向函数中传递的其他关键字参数

    例子

    # add a x -> x^2 layer
    model.add(Lambda(lambda x: x ** 2))
    
    # add a layer that returns the concatenation
    # of the positive part of the input and
    # the opposite of the negative part
    
    def antirectifier(x):
        x -= K.mean(x, axis=1, keepdims=True)
        x = K.l2_normalize(x, axis=1)
        pos = K.relu(x)
        neg = K.relu(-x)
        return K.concatenate([pos, neg], axis=1)
    
    def antirectifier_output_shape(input_shape):
        shape = list(input_shape)
        assert len(shape) == 2  # only valid for 2D tensors
        shape[-1] *= 2
        return tuple(shape)
    
    model.add(Lambda(antirectifier,
             output_shape=antirectifier_output_shape))
    

    输入shape

    任意,当使用该层作为第一层时,要指定input_shape

    输出shape

    output_shape参数指定的输出shape,当使用tensorflow时可自动推断


    ActivityRegularizer层

    keras.layers.core.ActivityRegularization(l1=0.0, l2=0.0)
    

    经过本层的数据不会有任何变化,但会基于其激活值更新损失函数值

    参数

    • l1:1范数正则因子(正浮点数)

    • l2:2范数正则因子(正浮点数)

    输入shape

    任意,当使用该层作为第一层时,要指定input_shape

    输出shape

    与输入shape相同


    Masking层

    keras.layers.core.Masking(mask_value=0.0)
    

    使用给定的值对输入的序列信号进行“屏蔽”,用以定位需要跳过的时间步

    对于输入张量的时间步,即输入张量的第1维度(维度从0开始算,见例子),如果输入张量在该时间步上都等于mask_value,则该时间步将在模型接下来的所有层(只要支持masking)被跳过(屏蔽)。

    如果模型接下来的一些层不支持masking,却接受到masking过的数据,则抛出异常。

    例子

    考虑输入数据x是一个形如(samples,timesteps,features)的张量,现将其送入LSTM层。因为你缺少时间步为3和5的信号,所以你希望将其掩盖。这时候应该:

    • 赋值x[:,3,:] = 0.x[:,5,:] = 0.

    • 在LSTM层之前插入mask_value=0.Masking

    model = Sequential()
    model.add(Masking(mask_value=0., input_shape=(timesteps, features)))
    model.add(LSTM(32))
    
    
    

     参考网址链接:https://keras-cn.readthedocs.io/en/latest/

    若有兴趣交流分享技术,可关注本人公众号,里面会不定期的分享各种编程教程,和共享源码,诸如研究分享关于c/c++,python,前端,后端,opencv,halcon,opengl,机器学习深度学习之类有关于基础编程,图像处理和机器视觉开发的知识

  • 相关阅读:
    变量的创建和初始化
    HDU 1114 Piggy-Bank (dp)
    HDU 1421 搬寝室 (dp)
    HDU 2059 龟兔赛跑 (dp)
    HDU 2571 命运 (dp)
    HDU 1574 RP问题 (dp)
    HDU 2577 How to Type (字符串处理)
    HDU 1422 重温世界杯 (dp)
    HDU 2191 珍惜现在,感恩生活 (dp)
    HH实习 acm算法部 1689
  • 原文地址:https://www.cnblogs.com/DOMLX/p/9769301.html
Copyright © 2011-2022 走看看