分享

常用层Core

 田杰4 2017-01-26

常用层

常用层对应于core模块,core内部定义了一系列常用的网络层,包括全连接、激活层等

Dense层

keras.layers.core.Dense(output_dim, init='glorot_uniform', activation='linear', weights=None, W_regularizer=None, b_regularizer=None, activity_regularizer=None, W_constraint=None, b_constraint=None, bias=True, input_dim=None)

Dense就是常用的全连接层,这里是一个使用示例:

# as first layer in a sequential model:model = Sequential()model.add(Dense(32, input_dim=16))# now the model will take as input arrays of shape (*, 16)# and output arrays of shape (*, 32)# this is equivalent to the above:model = Sequential()model.add(Dense(32, input_shape=(16,)))# after the first layer, you don't need to specify# the size of the input anymore:model.add(Dense(32))

参数:

  • output_dim:大于0的整数,代表该层的输出维度。模型中非首层的全连接层其输入维度可以自动推断,因此非首层的全连接定义时不需要指定输入维度。

  • init:初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的Theano函数。该参数仅在不传递weights参数时才有意义。

  • activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)

  • weights:权值,为numpy array的list。该list应含有一个形如(input_dim,output_dim)的权重矩阵和一个形如(output_dim,)的偏置向量。

  • W_regularizer:施加在权重上的正则项,为WeightRegularizer对象

  • b_regularizer:施加在偏置向量上的正则项,为WeightRegularizer对象

  • activity_regularizer:施加在输出上的正则项,为ActivityRegularizer对象

  • W_constraints:施加在权重上的约束项,为Constraints对象

  • b_constraints:施加在偏置上的约束项,为Constraints对象

  • bias:布尔值,是否包含偏置向量(即层对输入做线性变换还是仿射变换)

  • input_dim:整数,输入数据的维度。当Dense层作为网络的第一层时,必须指定该参数或input_shape参数。

输入

形如(nb_samples, ..., input_dim)的nD张量,最常见的情况为(nb_samples, input_dim)的2D张量

输出

形如(nb_samples, ..., output_dim)的nD张量,最常见的情况为(nb_samples, output_dim)的2D张量


Activation层

keras.layers.core.Activation(activation)

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

参数

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

输入shape

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

输出shape

与输入shape相同


Dropout层

keras.layers.core.Dropout(p)

为输入数据施加Dropout。Dropout将在训练过程中每次更新参数时随机断开一定百分比(p)的输入神经元连接,Dropout层用于防止过拟合。

参数

  • p:0~1的浮点数,控制需要断开的链接的比例

参考文献


SpatialDropout1D层

keras.layers.core.SpatialDropout1D(p)

SpatialDropout1D与Dropout的作用类似,但它断开的是整个1D特征图,而不是单个神经元。如果一张特征图的相邻像素之间有很强的相关性(通常发生在低层的卷积层中),那么普通的dropout无法正则化其输出,否则就会导致明显的学习率下降。这种情况下,SpatialDropout1D能够帮助提高特征图之间的独立性,应该用其取代普通的Dropout

参数

  • p:0~1的浮点数,控制需要断开的链接的比例

输入shape

输入形如(samples,timesteps,channels)的3D张量

输出shape

与输入相同

参考文献


SpatialDropout2D层

keras.layers.core.SpatialDropout2D(p, dim_ordering='default')

SpatialDropout2D与Dropout的作用类似,但它断开的是整个2D特征图,而不是单个神经元。如果一张特征图的相邻像素之间有很强的相关性(通常发生在低层的卷积层中),那么普通的dropout无法正则化其输出,否则就会导致明显的学习率下降。这种情况下,SpatialDropout2D能够帮助提高特征图之间的独立性,应该用其取代普通的Dropout

参数

  • p:0~1的浮点数,控制需要断开的链接的比例
  • dim_ordering:'th'或'tf',默认为~/.keras/keras.json配置的image_dim_ordering

输入shape

‘th’模式下,输入形如(samples,channels,rows,cols)的4D张量

‘tf’模式下,输入形如(samples,rows,cols,channels)的4D张量

注意这里的输入shape指的是函数内部实现的输入shape,而非函数接口应指定的input_shape,请参考下面提供的例子。

输出shape

与输入相同

参考文献


SpatialDropout3D层

keras.layers.core.SpatialDropout3D(p, dim_ordering='default')

SpatialDropout3D与Dropout的作用类似,但它断开的是整个3D特征图,而不是单个神经元。如果一张特征图的相邻像素之间有很强的相关性(通常发生在低层的卷积层中),那么普通的dropout无法正则化其输出,否则就会导致明显的学习率下降。这种情况下,SpatialDropout3D能够帮助提高特征图之间的独立性,应该用其取代普通的Dropout

参数

  • p:0~1的浮点数,控制需要断开的链接的比例
  • dim_ordering:'th'或'tf',默认为~/.keras/keras.json配置的image_dim_ordering

输入shape

‘th’模式下,输入应为形如(samples,channels,input_dim1,input_dim2, input_dim3)的5D张量

‘tf’模式下,输入应为形如(samples,input_dim1,input_dim2, input_dim3,channels)的5D张量

输出shape

与输入相同

参考文献


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 modelmodel = 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 modelmodel.add(Reshape((6, 2)))# now: model.output_shape == (None, 6, 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 dimensionmodel.add(RepeatVector(3))# now: model.output_shape == (None, 3, 32)

Merge层

keras.engine.topology.Merge(layers=None, mode='sum', concat_axis=-1, dot_axes=-1, output_shape=None, node_indices=None, tensor_indices=None, name=None)

Merge层根据给定的模式,将一个张量列表中的若干张量合并为一个单独的张量

参数

  • layers:该参数为Keras张量的列表,或Keras层对象的列表。该列表的元素数目必须大于1。
  • mode:合并模式,为预定义合并模式名的字符串或lambda函数或普通函数,如果为lambda函数或普通函数,则该函数必须接受一个张量的list作为输入,并返回一个张量。如果为字符串,则必须是下列值之一:

    • “sum”,“mul”,“concat”,“ave”,“cos”,“dot”
  • concat_axis:整数,当mode=concat时指定需要串联的轴

  • dot_axes:整数或整数tuple,当mode=dot时,指定要消去的轴

  • output_shape:整数tuple或lambda函数/普通函数(当mode为函数时)。如果output_shape是函数时,该函数的输入值应为一一对应于输入shape的list,并返回输出张量的shape。

  • node_indices:可选,为整数list,如果有些层具有多个输出节点(node)的话,该参数可以指定需要merge的那些节点的下标。如果没有提供,该参数的默认值为全0向量,即合并输入层0号节点的输出值。

  • tensor_indices:可选,为整数list,如果有些层返回多个输出张量的话,该参数用以指定需要合并的那些张量。

例子

model1 = Sequential()model1.add(Dense(32))model2 = Sequential()model2.add(Dense(32))merged_model = Sequential()merged_model.add(Merge([model1, model2], mode='concat', concat_axis=1)- ____TODO__: would this actually work? it needs to.__# achieve this with get_source_inputs in Sequential.

Lambda层

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

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

参数

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

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

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

例子

# add a x -> x^2 layermodel.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 partdef 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


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))

Highway层

keras.layers.core.Highway(init='glorot_uniform', transform_bias=-2, activation='linear', weights=None, W_regularizer=None, b_regularizer=None, activity_regularizer=None, W_constraint=None, b_constraint=None, bias=True, input_dim=None)

Highway层建立全连接的Highway网络,这是LSTM在前馈神经网络中的推广

参数:

  • output_dim:大于0的整数,代表该层的输出维度。模型中非首层的全连接层其输入维度可以自动推断,因此非首层的全连接定义时不需要指定输入维度。

  • init:初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的Theano函数。该参数仅在不传递weights参数时有意义。

  • activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)

  • weights:权值,为numpy array的list。该list应含有一个形如(input_dim,output_dim)的权重矩阵和一个形如(output_dim,)的偏置向量。

  • W_regularizer:施加在权重上的正则项,为WeightRegularizer对象

  • b_regularizer:施加在偏置向量上的正则项,为WeightRegularizer对象

  • activity_regularizer:施加在输出上的正则项,为ActivityRegularizer对象

  • W_constraints:施加在权重上的约束项,为Constraints对象

  • b_constraints:施加在偏置上的约束项,为Constraints对象

  • bias:布尔值,是否包含偏置向量(即层对输入做线性变换还是仿射变换)

  • input_dim:整数,输入数据的维度。当该层作为网络的第一层时,必须指定该参数或input_shape参数。

  • transform_bias:用以初始化传递参数,默认为-2(请参考文献理解本参数的含义)

输入shape

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

输出shape

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

参考文献


MaxoutDense层

全连接的Maxout层

MaxoutDense层以nb_featuresDense(input_dim,output_dim)线性层的输出的最大值为输出。MaxoutDense可对输入学习出一个凸的、分段线性的激活函数。

参数

  • nb_features:内部使用的全连接层的数目

输入shape

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

输出shape

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

参考文献


TimeDisributedDense层

keras.layers.core.TimeDistributedDense(output_dim, init='glorot_uniform', activation='linear', weights=None, W_regularizer=None, b_regularizer=None, activity_regularizer=None, W_constraint=None, b_constraint=None, bias=True, input_dim=None, input_length=None)

为输入序列的每个时间步信号(即维度1)建立一个全连接层,当RNN网络设置为return_sequence=True时尤其有用

  • 注意:该层已经被弃用,请使用其包装器TImeDistributed完成此功能
model.add(TimeDistributed(Dense(32)))

参数

  • output_dim:大于0的整数,代表该层的输出维度。模型中非首层的全连接层其输入维度可以自动推断,因此非首层的全连接定义时不需要指定输入维度。

  • init:初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的Theano函数。该参数仅在不传递weights参数时有意义。

  • activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)

  • weights:权值,为numpy array的list。该list应含有一个形如(input_dim,output_dim)的权重矩阵和一个形如(output_dim,)的偏置向量。

  • W_regularizer:施加在权重上的正则项,为WeightRegularizer对象

  • b_regularizer:施加在偏置向量上的正则项,为WeightRegularizer对象

  • activity_regularizer:施加在输出上的正则项,为ActivityRegularizer对象

  • W_constraints:施加在权重上的约束项,为Constraints对象

  • b_constraints:施加在偏置上的约束项,为Constraints对象

  • bias:布尔值,是否包含偏置向量(即层对输入做线性变换还是仿射变换)

  • input_dim:整数,输入数据的维度。当该层作为网络的第一层时,必须指定该参数或input_shape参数。

  • input_length:输入序列的长度,为整数或None,若为None则代表输入序列是变长序列

输入shape

形如 (nb_sample, time_dimension, input_dim)的3D张量

输出shape

形如 (nb_sample, time_dimension, output_dim)的3D张量

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多