本文来自CSDN博客:http://blog.csdn.net/niuwei22007/article/details/49277595 原博客可以获取更多文章
从这篇开始介绍Keras的Layers,就是构成网络的每一层。Keras实现了很多层,包括核心层、卷基层、RNN网络层等诸多常用的网络结构。下面开介绍核心层中包含了哪些内容。因为这个核心层我现在还没有全部用到,所以会有一部分内容我并不是十分了解,因此直接附带了原文档介绍。希望有了解的博友指点一二。
keras.layers.core.Layer()
下面介绍一下该类中包含的几个基本方法。
# 把previous_layer层的输出连接到当前层的输入 set_previous(previous_layer)
返回:None
参数:
# 获取某层网络的输出 get_output(train)
返回:Theano tensor
参数:
# 获取某层网络的输入 get_input(train)
返回:Theano tensor
参数:
# 获取网络的权值 get_weights()
# 设置网络权值参数 set_weights(weights)
参数:
get_config()
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, input_dim=None)
inputshape: 2维 tensor(nb_samples, input_dim)
outputshape: 2维 tensor(nb_samples, output_dim)
参数:
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, input_dim=None, input_length=None)
这是一个基于时间维度的全连接层。主要就是用来构建RNN(递归神经网络)的,但是在构建RNN时需要设置return_sequences=True。
inputshape: 3维 tensor(nb_samples, timesteps,input_dim)
参数:
简单举例:
# input shape: (nb_samples, timesteps,10) model.add(LSTM(5, return_sequences=True, input_dim=10)) # output shape: (nb_samples, timesteps, 5) model.add(TimeDistributedDense(15)) # output shape:(nb_samples, timesteps, 15)
keras.layers.core.AutoEncoder(encoder, decoder,output_reconstruction=True, weights=None)
这是一个用于构建很常见的自动编码模型。如果参数output_reconstruction=True,那么dim(input)=dim(output);否则dim(output)=dim(hidden)。
inputshape: 取决于encoder的定义
outputshape:取决于decoder的定义
参数:
简单举例:
from keras.layers import containers # input shape: (nb_samples, 32) encoder =containers.Sequential([Dense(16, input_dim=32), Dense(8)]) decoder =containers.Sequential([Dense(16, input_dim=8), Dense(32)]) autoencoder =Sequential() autoencoder.add(AutoEncoder(encoder=encoder, decoder=decoder,output_reconstruction=False))
keras.layers.core.Activation(activation)
Apply an activation function tothe input.(貌似是把激活函数应用到输入数据的一种层结构)
inputshape: 任意。当把这层作为某个模型的第一层时,需要用到该参数(元组,不包含样本轴)。
outputshape:同input shape
参数:
keras.layers.core.Dropout(p)
Dropout的意思就是训练和预测时随机减少特征个数,即去掉输入数据中的某些维度,用于防止过拟合。通过设置Dropout中的参数p,在训练和预测模型的时候,每次更新都会丢掉(总数*p)个特征,以达到防止过拟合的目的。可以参考:Dropout: A Simple Way to PreventNeural Networks from Overfitting
强烈推荐看一下文章《理解dropout》,可以使你更充分的理解dropout。
inputshape: 任意。当把这层作为某个模型的第一层时,需要用到该参数(元组,不包含样本轴)。
outputshape:同input shape
参数:
keras.layers.core.Reshape(dims)
就是把输入数据的shape重新reshape一下,原数据保持不变。
inputshape: 任意。当把这层作为某个模型的第一层时,需要用到该参数(元组,不包含样本轴)。
outputshape:(nb_samples, dims)
参数:
dims:整数型元组,新的shape。
简单举例:
# input shape: (nb_samples, 10) model.add(Dense(100, input_dim=10)) # output shape: (nb_samples, 100) model.add(Reshape(dims=(10, 10))) # output shape: (nb_samples, 10, 10)
keras.layers.core.Flatten()
把多维输入转换为1维输入,名字很形象,就是把输入给压平了。
inputshape: 任意。当把这层作为某个模型的第一层时,需要用到该参数(元组,不包含样本轴)。
outputshape:(nb_samples,nb_input_units)
keras.layers.core.RepeatVector(n)
把1维的输入重复n次。假设输入维度为(nb_samples, dim),那么输出shape就是(nb_samples, n, dim)
inputshape: 任意。当把这层作为某个模型的第一层时,需要用到该参数(元组,不包含样本轴)。
outputshape:(nb_samples,nb_input_units)
参数:
keras.layers.core.Permute(dims)
根据给定的元组交换输入数据维度。主要是用于RNNs和Convnets。
inputshape: 任意。当把这层作为某个模型的第一层时,需要用到该参数(元组,不包含样本轴)。
outputshape:和input shape一样。但是维度需要根据指定的元组顺序重新排序。
参数:元组,明确指出交换对象。例如(2,1)就是交换输入的第一维和第二维。
简单举例:
# input shape: (nb_samples, 10) model.add(Dense(50, input_dim=10)) # output shape: (nb_samples, 50) model.add(Reshape(dims=(10, 5))) # output shape:(nb_samples, 10, 5) model.add(Permute(dims=(2, 1))) #output shape: (nb_samples, 5, 10)
keras.layers.core.ActivityRegularization(l1=0., l2=0.)
保持输入不变,在代价函数基础上增加一项针对input activity的L1和L2规则化项。
这个层的可以用来降低前一层激活函数结果的稀疏性。
keras.layers.core.MaxoutDense(output_dim,nb_feature=4, init='glorot_uniform', weights=None, W_regularizer=None, b_regularizer=None, activity_regularizer=None, W_constraint=None, b_constraint=None, input_dim=None)
A dense maxout layer. AMaxoutDense layer takes the element-wise maximum of nb_feature Dense(input_dim, output_dim) linear layers. This allows thelayer to learn a convex, piecewise linear activation function over the inputs.See this paper for more details. Note that this is a linear layer -- if youwish to apply activation function (you shouldn't need to -- they are universalfunction approximators), an Activation layer must be added after.
inputshape:2维tensor,shape为(nb_samples, input_dim)。
outputshape:2维tensor,shape为(nb_samples, output_dim)。
简单举例:
# input shape: (nb_samples, 10) model.add(Dense(100, input_dim=10)) # output shape: (nb_samples, 100) model.add(MaxoutDense(50, nb_feature=10)) # output shape: (nb_samples, 50)
keras.layers.core.Merge(models, mode='sum')
把layers(or containers) list合并为一个层,用以下三种模式中的一种:sum,mul或 concat。
参数:
简单举例:
left = Sequential() left.add(Dense(50, input_shape=(784,))) left.add(Activation('relu')) right = Sequential() right.add(Dense(50, input_shape=(784,))) right.add(Activation('relu')) model = Sequential() model.add(Merge([left,right], mode='sum')) model.add(Dense(10)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit([X_train, X_train], Y_train, batch_size=128, nb_epoch=20, validation_data=([X_test, X_test], Y_test))
keras.layers.core.Masking(mask_value=0.)
Create a mask for the input databy usingmask_value as the sentinel value whichshould be masked out. Given an input of dimensions(nb_samples,timesteps, input_dim), return the input untouched as output, and supply a maskof shape (nb_samples, timesteps)where all timesteps which hadall their values equal to mask_value are masked out.
inputshape: 3D tensor with shape: (nb_samples, timesteps,features).
outputshape: 3D tensor with shape:(nb_samples, timesteps,features).