Keras学习笔记一:常用层keras.layers.core部分函数

1. Dense层:就是常用的全连接层,所实现的运算是output = activation(dot(input, kernel)+bias)。其中activation是逐元素计算的激活函数,kernel是本层的权值矩阵,bias为偏置向量,只有当use_bias=True才会添加。

如果本层的输入数据的维度大于2,则会先被压为与kernel相匹配的大小。

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对象

输入

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

输出

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

 

2. Activation层:keras.layers.core.Activation(activation)

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

参数

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

输入shape

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

输出shape

与输入shape相同

3.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:整数,使用的随机数种子

参考文献

·       Dropout: A SimpleWay to Prevent Neural Networks from Overfitting

4.Flatten层:keras.layers.core.Flatten()

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

5.Reshape层:keras.layers.core.Reshape(target_shape)

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

参数

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

输入shape

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

输出shape

(batch_size,)+target_shape

5.RepeateVector层:keras.layers.core.RepeatVector(n)

RepeatVector层将输入重复n

参数

·       n:整数,重复的次数

输入shape

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

输出shape

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

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

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

参数

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

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

·       mask:掩膜

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

 7. Keras添加层示例:

# as first layer in a sequential model:

# as first layer in a sequential model:

model = Sequential()

model.add(Dense(32, input_shape=(16,)))

# now the model will take as input arrays of shape (*, 16)
# and output arrays of shape (*, 32)
# after the first layer, you don't need to specify
# the size of the input anymore:
model.add(Dense(32))

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值