常用层常用层对应于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)) 参数:
输入形如(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) 激活层对一个层的输出施加激活函数 参数
输入shape任意,当使用激活层作为第一层时,要指定 输出shape与输入shape相同 Dropout层keras.layers.core.Dropout(p) 为输入数据施加Dropout。Dropout将在训练过程中每次更新参数时随机断开一定百分比(p)的输入神经元连接,Dropout层用于防止过拟合。 参数
参考文献SpatialDropout1D层keras.layers.core.SpatialDropout1D(p) SpatialDropout1D与Dropout的作用类似,但它断开的是整个1D特征图,而不是单个神经元。如果一张特征图的相邻像素之间有很强的相关性(通常发生在低层的卷积层中),那么普通的dropout无法正则化其输出,否则就会导致明显的学习率下降。这种情况下,SpatialDropout1D能够帮助提高特征图之间的独立性,应该用其取代普通的Dropout 参数
输入shape输入形如(samples,timesteps,channels)的3D张量 输出shape与输入相同 参考文献SpatialDropout2D层keras.layers.core.SpatialDropout2D(p, dim_ordering='default') SpatialDropout2D与Dropout的作用类似,但它断开的是整个2D特征图,而不是单个神经元。如果一张特征图的相邻像素之间有很强的相关性(通常发生在低层的卷积层中),那么普通的dropout无法正则化其输出,否则就会导致明显的学习率下降。这种情况下,SpatialDropout2D能够帮助提高特征图之间的独立性,应该用其取代普通的Dropout 参数
输入shape‘th’模式下,输入形如(samples,channels,rows,cols)的4D张量 ‘tf’模式下,输入形如(samples,rows,cols,channels)的4D张量 注意这里的输入shape指的是函数内部实现的输入shape,而非函数接口应指定的 输出shape与输入相同 参考文献SpatialDropout3D层keras.layers.core.SpatialDropout3D(p, dim_ordering='default') SpatialDropout3D与Dropout的作用类似,但它断开的是整个3D特征图,而不是单个神经元。如果一张特征图的相邻像素之间有很强的相关性(通常发生在低层的卷积层中),那么普通的dropout无法正则化其输出,否则就会导致明显的学习率下降。这种情况下,SpatialDropout3D能够帮助提高特征图之间的独立性,应该用其取代普通的Dropout 参数
输入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 参数
输入shape任意,但输入的shape必须固定。当使用该层为模型首层时,需要指定 输出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网络连接时,可能会用到该层。 参数
例子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任意,当使用激活层作为第一层时,要指定 输出shape与输入相同,但是其维度按照指定的模式重新排列 RepeatVector层keras.layers.core.RepeatVector(n) RepeatVector层将输入重复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层根据给定的模式,将一个张量列表中的若干张量合并为一个单独的张量 参数
例子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表达式 参数
例子# 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任意,当使用该层作为第一层时,要指定 输出shape由 ActivityRegularizer层keras.layers.core.ActivityRegularization(l1=0.0, l2=0.0) 经过本层的数据不会有任何变化,但会基于其激活值更新损失函数值 参数
输入shape任意,当使用该层作为第一层时,要指定 输出shape与输入shape相同 Masking层keras.layers.core.Masking(mask_value=0.0) 使用给定的值对输入的序列信号进行“屏蔽”,用以定位需要跳过的时间步 对于输入张量的时间步,即输入张量的第1维度(维度从0开始算,见例子),如果输入张量在该时间步上都等于 如果模型接下来的一些层不支持masking,却接受到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在前馈神经网络中的推广 参数:
输入shape形如(nb_samples, input_dim)的2D张量 输出shape形如(nb_samples, output_dim)的2D张量 参考文献MaxoutDense层全连接的Maxout层
参数
输入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网络设置为
model.add(TimeDistributed(Dense(32))) 参数
输入shape形如 输出shape形如 |
|