1. 高级激活层
1.1. LeakyReLU层
LeakyRelU是修正线性单元( Rectified Linear Unit, ReLU)的特殊版本,当不激活时, LeakyReLU仍然会有非零输出值,从而获得一个小梯度,避免ReLU可能出现的神经元“死亡”现象。
keras.layers.advanced_activations.LeakyReLU(alpha=0.3)
输出shape与输入相同
keras.layers.advanced_activations.PReLU(init='zero', weights=None)
keras.layers.advanced_activations.LeakyReLU(alpha=0.3)
- alpha:大于0的浮点数,代表激活函数图像中第三象限线段的斜率
输出shape与输入相同
1.2. PReLU层
该层为参数化的ReLU( Parametric ReLU),表达式是: f(x) = alpha * x for x < 0 , f(x) = x for x>=0 ,此处的 alpha 为一个与xshape相同的可学习的参数向量。keras.layers.advanced_activations.PReLU(init='zero', weights=None)
- init: alpha的初始化函数
- weights: alpha的初始化值,为具有单个numpy array的list
1.3. ELU层
ELU层是指数线性单元( Exponential Linera Unit),表达式为: 该层为参数化的ReLU( Parametric ReLU),表达式是: f(x) = alpha * (exp(x) - 1.) for x < 0 , f(x) = x for x>=0
keras.layers.advanced_activations.ELU(alpha=1.0)
keras.layers.advanced_activations.ELU(alpha=1.0)
1.4. ParametricSoftplus层
该层是参数化的Softplus,表达式是: f(x) = alpha * log(1 + exp(beta * x))keras.layers.advanced_activations.ParametricSoftplus(alpha_init=0.2, beta_init=5.0, weights=None)
- alpha_init:浮点数, alpha的初始值
- beta_init:浮点数, beta的初始值
- weights:初始化权重,为含有两个numpy array的list
1.5. ThresholdedReLU层
该层是带有门限的ReLU,表达式是: f(x) = x for x > theta , f(x) = 0 otherwise
keras.layers.advanced_activations.ThresholdedReLU(theta=1.0)
keras.layers.advanced_activations.ThresholdedReLU(theta=1.0)
- theata:大或等于0的浮点数,激活门限位置
1.6. SReLU层
该层是S形的ReLU
keras.layers.advanced_activations.SReLU(t_left_init='zero', a_left_init='glorot_uniform', t_right_init='glorot_uniform', a_right_init='one')
keras.layers.advanced_activations.SReLU(t_left_init='zero', a_left_init='glorot_uniform', t_right_init='glorot_uniform', a_right_init='one')
- t_left_init:左侧截断初始化函数
- a_left_init:左侧斜率初始化函数
- t_right_init:右侧截断初始化函数
- a_right_init:右侧斜率初始化函数
2. 规范化
2.1. BatchNormalization层
该层在每个batch上将前一层的激活值重新规范化,即使得其输出数据的均值接近0,其标准差接近1
keras.layers.normalization.BatchNormalization(epsilon=1e-06, mode=0, axis=-1, momentum=0.9, weights=None, beta_init='zero', gamma_init='one')
keras.layers.normalization.BatchNormalization(epsilon=1e-06, mode=0, axis=-1, momentum=0.9, weights=None, beta_init='zero', gamma_init='one')
- epsilon:大于0的小浮点数,用于防止除0错误
- mode:整数,指定规范化的模式,取0或1;0:按特征规范化,输入的各个特征图将独立被规范化。规范化的轴由参数 axis 指定。注意,如果输入是形如( samples, channels, rows, cols)的4D图像张量,则应设置规范化的轴为1,即沿着通道轴规范化。输入格式是‘tf’同理。1:按样本规范化,该模式默认输入为2D
- axis:整数,指定当 mode=0 时规范化的轴。例如输入是形如( samples, channels, rows, cols)的4D图像张量,则应设置规范化的轴为1,意味着对每个特征图进行规范化
- momentum:在按特征规范化时,计算数据的指数平均数和标准差时的动量
- weights:初始化权重,为包含2个numpy array的list,其shape为 [(input_shape,),(input_shape)]
- beta_init: beta的初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的Theano函数。该参数仅在不传递 weights 参数时有意义。
- gamma_init: gamma的初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的Theano函数。该参数仅在不传递 weights 参数时有意义。
统计学习的一个重要假设是源空间与目标空间的数据分布是一致的,而神经网络各层输出的分布不一定与输入一致,尤其当网络越深,这种不一致越明显。 BatchNormalization把分布一致弱化为均值与方差一致,然而即使是这种弱化的版本也对学习过程起到了重要效果。另一方面, BN的更重要作用是防止梯度弥散,它通过将激活值规范为统一的均值和方差,将原本会减小的激活值得到放大。
3. 噪声层Noise
3.1. GaussianNoise层
为层的输入施加0均值,标准差为 sigma 的加性高斯噪声。该层在克服过拟合时比较有用,你可以将它看作是随机的数据提升。高斯噪声是需要对输入数据进行破坏时的自然选择。因为这是一个起正则化作用的层,该层只在训练时才有效。keras.layers.noise.GaussianNoise(sigma)
- sigma:浮点数,代表要产生的高斯噪声标准差
3.2. GaussianDropout层
为层的输入施加以1为均值,标准差为 sqrt(p/(1-p) 的乘性高斯噪声
keras.layers.noise.GaussianDropout(p)
keras.layers.noise.GaussianDropout(p)
- p:浮点数,断连概率,与Dropout层相同
4. 包装器Wrapper
4.1. TimeDistributed包装器
该包装器可以把一个层应用到输入的每一个时间步上
keras.layers.wrappers.TimeDistributed(layer)
keras.layers.wrappers.TimeDistributed(layer)
- layer: Keras层对象
# as the first layer in a model
model = Sequential()
model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))
# now model.output_shape == (None, 10, 8)4.2. Bidirectional包装器
双向RNN包装器
keras.layers.wrappers.Bidirectional(layer, merge_mode='concat', weights=None)
keras.layers.wrappers.Bidirectional(layer, merge_mode='concat', weights=None)
- layer: Recurrent 对象
- merge_mode:前向和后向RNN输出的结合方式,为 sum , mul , concat , ave 和 None 之一,若设为None,则返回值不结合,而是以列表的形式返回
model = Sequential()
model.add(Bidirectional(LSTM(10, return_sequences=True), input_shape=(5, 10)))
model.add(Bidirectional(LSTM(10)))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')5. 自定义层
这里是一个Keras层应该具有的框架结构,要定制自己的层,你需要实现下面三个方法:
- build(input_shape) :这是定义权重的方法,可训练的权应该在这里被加入列表 `self.trainable_weights 中。其他的属性还包括 self.non_trainabe_weights (列表)和 self.updates (需要更新的形如( tensor, new_tensor)的tuple的列表)。你可以参考 BatchNormalization 层的实现来学习如何使用上面两个属性。这个方法必须设置 self.built = True ,可通过调用 uper([layer],self).build() 实现
- call(x) :这是定义层功能的方法,除非你希望你写的层支持masking,否则你只需要关心 call 的第一个参数:输入张量
- get_output_shape_for(input_shape) :如果你的层修改了输入数据的shape,你应该在这里指定shape变化的方法,这个函数使得Keras可以做自动shape推断
from keras import backend as K
from keras.engine.topology import Layer
class MyLayer(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
input_dim = input_shape[1]
initial_weight_value = np.random.random((input_dim, output_dim))
self.W = K.variable(initial_weight_value)
self.trainable_weights = [self.W]
super(MyLayer, self).build() # be sure you call this somewhere!
def call(self, x, mask=None):
return K.dot(x, self.W)
def get_output_shape_for(self, input_shape):
return (input_shape[0] + self.output_dim)
本文介绍了Keras中的一些高级激活层,包括LeakyReLU、PReLU等,详细讲解了每个层的参数含义。接着讨论了BatchNormalization层的规范化方法,以及GaussianNoise和GaussianDropout层引入噪声的作用。此外,还探讨了TimeDistributed和Bidirectional包装器的使用,以及自定义层的构建方法。

1万+

被折叠的 条评论
为什么被折叠?



