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
σB′2 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]×(1−momentum),
σ 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). σB′2[t]=σB′2[t]×momentum+σB2[t]×(1−momentum).
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=1∑Kxi,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=1∑K(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=1∑Hk=1∑Hxi,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=1∑Hk=1∑H(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.