论文笔记-batch,layer,weights normalization
paper:
Batch Normalization
在之前的笔记已经详细看过了:深度学习-Batch Normalization
Layer Normalization
Motivation
batch normalization uses the distribution of the summed input to a neuron over a mini-batch of training cases to compute a mean and variance which are then used to normalize the summed input to that neuron on each training case.
关于 batch normalisztion.
从 Ng 的课上截来的一张图,全链接层相比卷积层更容易理解点,但形式上是一样的.
样本数量是 m,第 l 层经过激活函数输出是第 l+1 层的输入,其中第 i 个神经元的值:
线性输出: $z_i^l={w_i^l}^Th^l$.
非线性输出: $h_i^{l+1} = a_i^l=f(z_i^l+b_i^l)$
其中 f 是非线性激活函数,$a_i^l$ 是下一层的 summed inputs. 如果 $a_i^l$ 的分布变化较大(change in a highly correlated way),下一层的权重 $w^{l+1}$ 的梯度也会相应变化很大(反向传播中 $w^{l+1}$ 的梯度就是 $a_i^l$)。
Batch Normalization 就是将线性输出归一化。
其中 $u_i^l$ 是均值,$\sigma_i^l$ 是方差。 $\overline a_i^l$ 是归一化之后的输出。 $g_i^l$ 是需要学习的参数,也就是 scale.
有个疑问?为什么 BN 要在激活函数之前进行,而不是之后进行呢?
上图中是单个样本,而所有的样本其实是共享层与层之间的参数的。样本与样本之间也存在差异,所以在某一个特征维度上进行归一化,(每一层其中的一个神经元可以看作一个特征维度)。
batch normalization requires running averages of the summed input statistics. In feed-forward networks with fixed depth, it is straightforward to store the statistics separately for each hidden layer. However, the summed inputs to the recurrent neurons in a recurrent neural network (RNN) often vary with the length of the sequence so applying batch normalization to RNNs appears to require different statistics for different time-steps.
BN 不是用于 RNN 是因为 batch 中的 sentence 长度不一致。我们可以把每一个时间步看作一个维度的特征提取,如果像 BN 一样在这个维度上进行归一化,显然在 RNN 上是行不通的。比如这个 batch 中最长的序列的最后一个时间步,他的均值就是它本身了,岂不是出现了 BN 在单个样本上训练的情况。
In this paper, we transpose batch normalization into layer normalization by computing the mean and variance used for normalization from all of the summed inputs to the neurons in a layer on a single training case.
所以作者在这篇 paper 中提出了 Layer Normalization. 在单个样本上计算均值和方差进行归一化。然而是怎么进行的呢?
Layer Normalization
layer normalization 并不是在样本上求平均值和方差,而是在 hidden units 上求平均值和方差。
其中 H 是 hidden units 的个数。
BN 和 LN 的差异:
Layer normalisztion 在单个样本上取均值和方差,所以在训练和测试阶段都是一致的。
并且,尽管求均值和方差的方式不一样,但是在转换成 beta 和 gamma 的方式是一样的,都是在 channels 或者说 hidden_size 上进行的。
Layer normalized recurrent neural networks
RNN is common among the NLP tasks to have different sentence lengths for different training cases. This is easy to deal with in an RNN because the same weights are used at every time-step. But when we apply batch normalization to an RNN in the obvious way, we need to to compute and store separate statistics for each time step in a sequence. This is problematic if a test sequence is longer than any of the training sequences. Layer normalization does not have such problem because its normalization terms depend only on the summed inputs to a layer at the current time-step. It also has only one set of gain and bias parameters shared over all time-steps.
这一部分也解释了 BN 不适用于 RNN 的原因,从 test sequence longer 的角度。RNN 的每个时间步计算共享参数权重.
$a^t=W_{hh}h^{t-1}+W_{xh}x^t$
其中 b 和 g 是可学习的参数。
layer normalize 在 LSTM 上的使用:
tensorflow 实现
batch Normalization
1 |
|
layer normalization
1 |
|
1 |
|
发现一个很奇怪的问题, layer norm 是在每一个训练样本上求均值和方差,为啥 beta 和 gamma 的shape却是 [hidden_size]. 按理说不应该是 [batch,] 吗? 带着疑问去看了源码,原来是这样的。。
将源码用简介的方式写出来了:
1 |
|
layer_norm_mine 得到的结果与源码一致。可以发现 计算均值和方差时, tf.nn.moments
中 axes=[1:-1]
. (tf.nn.moments 中 axes 的含义是在这些维度上求均值和方差). 也就是说得到的均值和方差确实是 [batch,]. 只是在转换成 beta 和 gamma 的分布时,依旧是在最后一个维度上进行的。有意思,所以最终的效果应该和 batch normalization 效果是一致的。只不过是否符合图像或文本的特性就另说了。
LayerNormBasicLSTMCell
1 |
|
论文笔记-batch,layer,weights normalization
http://www.panxiaoxie.cn/2018/10/01/论文笔记-batch-layer-weights-normalization/