Normalization Techniques: BN and LN

归一化在深度学习中扮演重要角色,能减少内部协变量偏移,平滑损失曲面,加快优化过程。本文介绍了批标准化(BN)和层标准化(LN)。批标准化在训练阶段通过计算小批量数据的均值和方差来规范化激活。在推断阶段使用训练期间计算的运行均值和方差。层标准化则针对每个样本进行归一化,特别适合于序列数据,如RNN。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Normalization Techniques

Motivation: Why needs Normalization?

Normalization has always been an active area of research in deep learning. There are some benefits of it:

  • It reduces Internal Covariate Shift. It is the change in the distribution of network activations due to the change in network parameters during training. It will move many of the distribution of network activations into the saturated regime of the nonlinearity and slow down the convergence. To improve training, we seek to reduce the internal covariate shift.
  • Normalization makes loss surface smoother.
  • It makes the Optimization faster because normalization doesn’t allow weights to explode all over the place and restricts them to a certain range.
Batch Normalization
Training Phase

Batch normalization is a method that normalizes activations in a network across the mini-batch of definite size. For each feature, batch normalization computes the mean and variance of that feature in the mini-batch . It then substracts the mean and divides the feature by its mini-batch standard deviation.

However, simply normalizing each input of a layer may change what the layer can represent. For instance, normalizing the inputs of a sigmoid would constrain them to the linear regime of the nonlinearity. To address this and make sure the transformation inserted in the network can represent the identity transform, a pair of parameters γ ( k ) , β ( k ) \gamma^{(k)},\beta^{(k)} γ(k),β(k) where introduced to scale and shift the normalized value for each activation x ( k ) x^{(k)} x(k):
y ( k ) = γ ( k ) x ^ ( k ) + β ( k ) y^{(k)} = \gamma^{(k)} \hat x^{(k)} + \beta^{(k)} y(k)=γ(k)x^(k)+β(k)
These parameters are learned along with the original model parameters, and restore the representation power of the network. Indeed, by setting γ ( k ) = V a r [ x ( k ) ] \gamma^{(k)}=\sqrt {Var[x^{(k)}]} γ(k)=Var[x(k)] and β ( k ) = E [ x ( k ) ] \beta^{(k)}=E[x^{(k)}] β(k)=E[x(k)], we could recover the original activatioins, if that were the optimal thing to do.

The batch normalization can be summarized as:

Inference Phase

During the inference phase, specifically when you only have one test sample, doing batch normalization as the one in training phase does not make sense, because your outputs at each layer of the network will be exactly zero. Instead. we use running mean and running variance calculated during training.

At each time step t t t during training, the runing mean μ B ′ [ t ] \mu_B'[t] μB[t] and running variance σ B ′ 2 \sigma_B'^2 σB2 are calculated as:
μ B ′ [ t ] = μ B ′ [ t ] × m o m e n t u m + μ B [ t ] × ( 1 − m o m e n t u m ) , \mu_B'[t]=\mu_B'[t]\times momentum+\mu_B[t]\times(1-momentum), μB[t]=μB[t]×momentum+μB[t]×(1momentum),

σ B ′ 2 [ t ] = σ B ′ 2 [ t ] × m o m e n t u m + σ B 2 [ t ] × ( 1 − m o m e n t u m ) . \sigma_B'^2[t]=\sigma_B'^2[t]\times momentum+\sigma_B^2[t]\times(1-momentum). σB2[t]=σB2[t]×momentum+σB2[t]×(1momentum).

Layer Normalization

Given inputs x x x over a minibatch of size m m m, B = { x 1 , x 2 , . . . , x m } B=\{x_1,x_2,...,x_m\} B={x1,x2,...,xm}, each sample x i x_i xi contains K K K elements, i.e. the length of flatten x i x_i xi is K K K, by applying transformation of your inputs using some learned parameters γ \gamma γ and β \beta β, the outputs could be expressed as B ′ = { y 1 , y 2 , . . . , y m } B'=\{y_1,y_2,...,y_m\} B={y1,y2,...,ym}, where y i = L N γ , β ( x i ) y_i=LN_{\gamma,\beta}(x_i) yi=LNγ,β(xi).

More concretely, we first calculate the mean and the variance of each sample from the minibatch. For sample x i x_i xi whose flatten format is { x i , 1 , x i , 2 , . . . , x i , K } \{x_{i,1},x_{i,2},...,x_{i,K}\} {xi,1,xi,2,...,xi,K}, we have its mean μ i \mu_i μi and variance σ i 2 \sigma_i^2 σi2.
μ i = 1 K ∑ k = 1 K x i , k , \mu_i={1\over K}\sum_{k=1}^Kx_{i,k}, μi=K1k=1Kxi,k,

σ i 2 = 1 K ∑ k = 1 K ( x i , k − μ i ) 2 . \sigma_i^2={1\over K}\sum_{k=1}^K(x_{i,k}-\mu_i)^2. σi2=K1k=1K(xi,kμi)2.

Then we normalize each sample such that the elements in the sample have zero mean and unit variance. ϵ \epsilon ϵ is for numerical stability in case the denominator becomes zero by chance:
x ^ i , k = x i , k − μ i σ i 2 + ϵ . \hat x_{i,k}={{x_{i,k} - \mu_i} \over {\sqrt{\sigma_i^2+\epsilon}}}. x^i,k=σi2+ϵ xi,kμi.
Finally, there is a scaling and shifting step like in batch normalization. γ \gamma γ and β \beta β are learnable parameters.
y i = γ x ^ i + β ≡ L N γ , β ( x i ) . y_i = \gamma \hat x_i + \beta \equiv LN_{\gamma,\beta}(x_i). yi=γx^i+βLNγ,β(xi).
Layer normalization has nothing to do with other sample in a mini batch.

Layer normalization for CNN

For CNN, each channel is considered as an “independent” sample and all the normalization was done for that specific channel only within the sample.

Assume the input tensor has shape [ b , H , W , C ] [b,H,W,C] [b,H,W,C], for each channel c ∈ { 1 , 2 , . . . , C } c\in \{1,2,...,C\} c{1,2,...,C}
μ i , c = 1 H W ∑ j = 1 H ∑ k = 1 H x i , j , k , c , \mu_{i,c}={1\over {HW}}\sum_{j=1}^H\sum_{k=1}^Hx_{i,j,k,c}, μi,c=HW1j=1Hk=1Hxi,j,k,c,

σ i , c 2 = 1 H W ∑ j = 1 H ∑ k = 1 H ( x i , j , k , c − μ i , c ) 2 , \sigma^2_{i,c}={1\over {HW}}\sum_{j=1}^H\sum_{k=1}^H(x_{i,j,k,c}-\mu_{i,c})^2, σi,c2=HW1j=1Hk=1H(xi,j,k,cμi,c)2,

x ^ i , j , k , c = x i , j , k , c − μ i , c σ i , c 2 + ϵ . \hat x_{i,j,k,c}={{x_{i,j,k,c}-\mu_{i,c}}\over {\sqrt{\sigma_{i,c}^2+\epsilon}}}. x^i,j,k,c=σi,c2+ϵ xi,j,k,cμi,c.

Similarly, we have learnable parameters γ c \gamma_c γc and β c \beta_c βc for each channel.

References
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值