Batch normalization explained

Batch normalization (also known as batch norm) is a method used to make training of artificial neural networks faster and more stable through normalization of the layers' inputs by re-centering and re-scaling. It was proposed by Sergey Ioffe and Christian Szegedy in 2015.[1]

While the effect of batch normalization is evident, the reasons behind its effectiveness remain under discussion. It was believed that it can mitigate the problem of internal covariate shift, where parameter initialization and changes in the distribution of the inputs of each layer affect the learning rate of the network. Recently, some scholars have argued that batch normalization does not reduce internal covariate shift, but rather smooths the objective function, which in turn improves the performance.[2] However, at initialization, batch normalization in fact induces severe gradient explosion in deep networks, which is only alleviated by skip connections in residual networks.[3] Others maintain that batch normalization achieves length-direction decoupling, and thereby accelerates neural networks.[4]

Internal covariate shift

Each layer of a neural network has inputs with a corresponding distribution, which is affected during the training process by the randomness in the parameter initialization and the randomness in the input data. The effect of these sources of randomness on the distribution of the inputs to internal layers during training is described as internal covariate shift. Although a clear-cut precise definition seems to be missing, the phenomenon observed in experiments is the change on means and variances of the inputs to internal layers during training.

Batch normalization was initially proposed to mitigate internal covariate shift. During the training stage of networks, as the parameters of the preceding layers change, the distribution of inputs to the current layer changes accordingly, such that the current layer needs to constantly readjust to new distributions. This problem is especially severe for deep networks, because small changes in shallower hidden layers will be amplified as they propagate within the network, resulting in significant shift in deeper hidden layers. Therefore, the method of batch normalization is proposed to reduce these unwanted shifts to speed up training and to produce more reliable models.

Besides reducing internal covariate shift, batch normalization is believed to introduce many other benefits. With this additional operation, the network can use higher learning rate without vanishing or exploding gradients. Furthermore, batch normalization seems to have a regularizing effect such that the network improves its generalization properties, and it is thus unnecessary to use dropout to mitigate overfitting. It has also been observed that the network becomes more robust to different initialization schemes and learning rates while using batch normalization.

Procedures

Transformation

In a neural network, batch normalization is achieved through a normalization step that fixes the means and variances of each layer's inputs. Ideally, the normalization would be conducted over the entire training set, but to use this step jointly with stochastic optimization methods, it is impractical to use the global information. Thus, normalization is restrained to each mini-batch in the training process.

Let us use B to denote a mini-batch of size m of the entire training set. The empirical mean and variance of B could thus be denoted as

\muB=

1
m
m
\sum
i=1

xi

and
2
\sigma
B

=

1
m
m
\sum
i=1

(xi-\mu

2
B)
.

For a layer of the network with d-dimensional input,

x=(x(1),...,x(d))

, each dimension of its input is then normalized (i.e. re-centered and re-scaled) separately,
(k)
\hat{x}
i

=

(k)
x
(k)
-\mu
B
i
\sqrt{\left(\sigma
(k)
B

\right)2+\epsilon}

, where

k\in[1,d]

and

i\in[1,m]

;
(k)
\mu
B
and
(k)
\sigma
B
are the per-dimension mean and standard deviation, respectively.

\epsilon

is added in the denominator for numerical stability and is an arbitrarily small constant. The resulting normalized activation

\hat{x}(k)

have zero mean and unit variance, if

\epsilon

is not taken into account. To restore the representation power of the network, a transformation step then follows as
(k)
y
i

=\gamma(k)

(k)
\hat{x}
i

+\beta(k)

,

where the parameters

\gamma(k)

and

\beta(k)

are subsequently learned in the optimization process.

Formally, the operation that implements batch normalization is a transform

BN
\gamma(k),\beta(k)

:

(k)
x
1...m

(k)
y
1...m
called the Batch Normalizing transform. The output of the BN transform

y(k)=

BN
\gamma(k),\beta(k)

(x(k))

is then passed to other network layers, while the normalized output
(k)
\hat{x}
i
remains internal to the current layer.

Backpropagation

The described BN transform is a differentiable operation, and the gradient of the loss l with respect to the different parameters can be computed directly with the chain rule.

Specifically,

\partiall
\partial
(k)
y
i

depends on the choice of activation function, and the gradient against other parameters could be expressed as a function of
\partiall
\partial
(k)
y
i

:
\partiall
\partial\hat{x
(k)
i
} = \frac\gamma^ ,
\partiall
\partial\gamma(k)

=

m
\sum
i=1
\partiall
\partial
(k)
y
i
(k)
\hat{x}
i

,
\partiall
\partial\beta(k)

=

m
\sum
i=1
\partiall
\partial
(k)
y
i

,
\partiall
\partial
(k)2
\sigma
B

=

m
\sum
i=1
\partiall
\partial
(k)
y
i
(k)
(x
i
(k)
-\mu)\left(-
B
\gamma(k)
2
(k)2
(\sigma
B

+\epsilon)-3/2\right)

,
\partiall
\partial
(k)
\mu
B

=

m
\sum
i=1
\partiall
\partial
(k)
y
i
-\gamma(k)
(k)2
\sqrt{\sigma+\epsilon
B
}+\frac\frac\sum_^m (-2)\cdot (x_i^-\mu^_B) ,

and

\partiall
\partial
(k)
x
i

=

\partiall
\partial\hat{x
(k)
i}1
(k)2
\sqrt{\sigma+\epsilon
B
}+\frac\frac+\frac\frac .

Inference

During the training stage, the normalization steps depend on the mini-batches to ensure efficient and reliable training. However, in the inference stage, this dependence is not useful any more. Instead, the normalization step in this stage is computed with the population statistics such that the output could depend on the input in a deterministic manner. The population mean,

E[x(k)]

, and variance,

\operatorname{Var}[x(k)]

, are computed as:

E[x(k)]=EB

(k)
[\mu
B]

, and

\operatorname{Var}[x(k)]=

m
m-1

EB

(k)
[\left(\sigma
B

\right)2]

.

The population statistics thus is a complete representation of the mini-batches.

The BN transform in the inference step thus becomes

y(k)=

inf
BN
\gamma(k),\beta(k)

(x(k))=\gamma(k)

x(k)-E[x(k)]
\sqrt{\operatorname{Var

[x(k)]+\epsilon}}+\beta(k)

,

where

y(k)

is passed on to future layers instead of

x(k)

. Since the parameters are fixed in this transformation, the batch normalization procedure is essentially applying a linear transform to the activation.

Theory

Although batch normalization has become popular due to its strong empirical performance, the working mechanism of the method is not yet well-understood. The explanation made in the original paper was that batch norm works by reducing internal covariate shift, but this has been challenged by more recent work. One experiment[2] trained a VGG-16 network[5] under 3 different training regimes: standard (no batch norm), batch norm, and batch norm with noise added to each layer during training. In the third model, the noise has non-zero mean and non-unit variance, i.e. it explicitly introduces covariate shift. Despite this, it showed similar accuracy to the second model, and both performed better than the first, suggesting that covariate shift is not the reason that batch norm improves performance.

Using batch normalization causes the items in a batch to no longer be iid, which can lead to difficulties in training due to lower quality gradient estimation.[6]

Smoothness

One alternative explanation, is that the improvement with batch normalization is instead due to it producing a smoother parameter space and smoother gradients, as formalized by a smaller Lipschitz constant.

Consider two identical networks, one contains batch normalization layers and the other does not, the behaviors of these two networks are then compared. Denote the loss functions as

\hat{L}

and

L

, respectively. Let the input to both networks be

x

, and the output be

y

, for which

y=Wx

, where

W

is the layer weights. For the second network,

y

additionally goes through a batch normalization layer. Denote the normalized activation as

\hat{y}

, which has zero mean and unit variance. Let the transformed activation be

z=\gamma\hat{y}+\beta

, and suppose

\gamma

and

\beta

are constants. Finally, denote the standard deviation over a mini-batch

\hat{yj}\in\Rm

as

\sigmaj

.

First, it can be shown that the gradient magnitude of a batch normalized network,

||\triangledown
yi

\hat{L}||

, is bounded, with the bound expressed as
||\triangledown
yi

\hat{L}||2\le

\gamma2
2
\sigma
j
(||\triangledown
yi
2-1
m
L||

\langle

1,\triangledown
yi
2-1
m
L\rangle
\langle\triangledown
yi
2)
L,\hat{y}
j\rangle

.

Since the gradient magnitude represents the Lipschitzness of the loss, this relationship indicates that a batch normalized network could achieve greater Lipschitzness comparatively. Notice that the bound gets tighter when the gradient

\triangledown
yi

\hat{L}

correlates with the activation

\hat{yi}

, which is a common phenomena. The scaling of
\gamma2
2
\sigma
j

is also significant, since the variance is often large.

Secondly, the quadratic form of the loss Hessian with respect to activation in the gradient direction can be bounded as

(\triangledown
yj

\hat{L})T

\partial\hat{L
}(\triangledown_\hat) \le \frac\bigg(\frac\bigg)^T \bigg(\frac\bigg)\bigg(\frac\bigg)-\frac\langle\triangledown_L,\hat\rangle \bigg|\bigg|\frac\bigg|\bigg|^2 .

The scaling of

\gamma2
2
\sigma
j

indicates that the loss Hessian is resilient to the mini-batch variance, whereas the second term on the right hand side suggests that it becomes smoother when the Hessian and the inner product are non-negative. If the loss is locally convex, then the Hessian is positive semi-definite, while the inner product is positive if

\hat{gj}

is in the direction towards the minimum of the loss. It could thus be concluded from this inequality that the gradient generally becomes more predictive with the batch normalization layer.

It then follows to translate the bounds related to the loss with respect to the normalized activation to a bound on the loss with respect to the network weights:

\hat{gj}\le

\gamma2
2
\sigma
j
2
(g
gj

2\langle

\triangledown
yj
2)
L,\hat{y}
j\rangle

, where

gj=max||X||\leλ||\triangledownWL||2

and

\hat{g}j=max||X||\leλ||\triangledownW\hat{L}||2

.

In addition to the smoother landscape, it is further shown that batch normalization could result in a better initialization with the following inequality:

*||
||W
0-\hat{W}

2\le

*||
||W
0-W
2-1
||W*||2

(||W*||2-\langle

2
W
0\rangle)

, where

W*

and

\hat{W}*

are the local optimal weights for the two networks, respectively.

Some scholars argue that the above analysis cannot fully capture the performance of batch normalization, because the proof only concerns the largest eigenvalue, or equivalently, one direction in the landscape at all points. It is suggested that the complete eigenspectrum needs to be taken into account to make a conclusive analysis.

Measure

Since it is hypothesized that batch normalization layers could reduce internal covariate shift, an experiment is set up to measure quantitatively how much covariate shift is reduced. First, the notion of internal covariate shift needs to be defined mathematically. Specifically, to quantify the adjustment that a layer's parameters make in response to updates in previous layers, the correlation between the gradients of the loss before and after all previous layers are updated is measured, since gradients could capture the shifts from the first-order training method. If the shift introduced by the changes in previous layers is small, then the correlation between the gradients would be close to 1.

The correlation between the gradients are computed for four models: a standard VGG network,[5] a VGG network with batch normalization layers, a 25-layer deep linear network (DLN) trained with full-batch gradient descent, and a DLN network with batch normalization layers. Interestingly, it is shown that the standard VGG and DLN models both have higher correlations of gradients compared with their counterparts, indicating that the additional batch normalization layers are not reducing internal covariate shift.

Vanishing/exploding gradients

Even though batchnorm was originally introduced to alleviate gradient vanishing or explosion problems, a deep batchnorm network in fact suffers from gradient explosion at initialization time, no matter what it uses for nonlinearity. Thus the optimization landscape is very far from smooth for a randomly initialized, deep batchnorm network.More precisely, if the network has

L

layers, then the gradient of the first layer weights has norm

>cλL

for some

λ>1,c>0

depending only on the nonlinearity.For any fixed nonlinearity,

λ

decreases as the batch size increases. For example, for ReLU,

λ

decreases to

\pi/(\pi-1)1.467

as the batch size tends to infinity.Practically, this means deep batchnorm networks are untrainable.This is only relieved by skip connections in the fashion of residual networks.

This gradient explosion on the surface contradicts the smoothness property explained in the previous section, but in fact they are consistent. The previous section studies the effect of inserting a single batchnorm in a network, while the gradient explosion depends on stacking batchnorms typical of modern deep neural networks.

Decoupling

Another possible reason for the success of batch normalization is that it decouples the length and direction of the weight vectors and thus facilitates better training.

By interpreting batch norm as a reparametrization of weight space, it can be shown that the length and the direction of the weights are separated and can thus be trained separately. For a particular neural network unit with input

x

and weight vector

w

, denote its output as

f(w)=

Tw)]
E
x[\phi(x

, where

\phi

is the activation function, and denote

S=E[xxT]

. Assume that

E[x]=0

, and that the spectrum of the matrix

S

is bounded as

0<\mu=λmin(S)

,

Lmax(S)<infty

, such that

S

is symmetric positive definite. Adding batch normalization to this unit thus results in

fBN(w,\gamma,\beta)=

Tw))]
E
x[\phi(BN(x

=

E
x[\phi(\gamma(
Tw]
x
x[x
Tw]
var1/2
x[x

)+\beta)]

, by definition.

The variance term can be simplified such that

Tw]=w
var
x[x

TSw

. Assume that

x

has zero mean and

\beta

can be omitted, then it follows that

fBN(w,\gamma)=

E
x[\phi(\gammaxTw
(wTSw)1/2

)]

, where

(wTSw)

1
2

is the induced norm of

S

,

||w||s

.

Hence, it could be concluded that

fBN(w,\gamma)=

T\tilde{w})]
E
x[\phi(x

, where

\tilde{w}=\gamma

w
||w||s

, and

\gamma

and

w

accounts for its length and direction separately. This property could then be used to prove the faster convergence of problems with batch normalization.

Linear convergence

Least-square problem

With the reparametrization interpretation, it could then be proved that applying batch normalization to the ordinary least squares problem achieves a linear convergence rate in gradient descent, which is faster than the regular gradient descent with only sub-linear convergence.

Denote the objective of minimizing an ordinary least squares problem as

min\tilde{w\in

d}f
R
OLS

(\tilde{w})=min\tilde{w\in

d}(E
R
x,y

[(y-xT\tilde{w})

2])=min
\tilde{w

\inRd}(2uT\tilde{w}+\tilde{w}TS\tilde{w})

, where

u=E[-yx]

and

S=E[xxT]

.

Since

\tilde{w}=\gammaw
||w||s

, the objective thus becomes
min
w\inRd\backslash\{0\

,\gamma\inR}fOLS

(w,\gamma)=min
w\inRd\backslash\{0\

,\gamma\inR}(2\gamma

uTw
2
||w||
S+\gamma

)

, where 0 is excluded to avoid 0 in the denominator.

Since the objective is convex with respect to

\gamma

, its optimal value could be calculated by setting the partial derivative of the objective against

\gamma

to 0. The objective could be further simplified to be
min
w\inRd\backslash\{0\
}\rho(w)=min_\bigg(-\frac\bigg) .

Note that this objective is a form of the generalized Rayleigh quotient

\tilde{\rho}(w)=wTBw
wTAw

, where

B\inRd

is a symmetric matrix and

A\inRd x

is a symmetric positive definite matrix.

It is proven that the gradient descent convergence rate of the generalized Rayleigh quotient is

λ1-\rho(wt+1)
\rho(wt+12)

\le(1-

λ1-λ2
λ1-λmin

)2t

λ1-\rho(wt)
\rho(wt)2

, where

λ1

is the largest eigenvalue of

B

,

λ2

is the second largest eigenvalue of

B

, and

λmin

is the smallest eigenvalue of

B

.[7]

In our case,

B=uuT

is a rank one matrix, and the convergence result can be simplified accordingly. Specifically, consider gradient descent steps of the form

wt+1=wtt\triangledown\rho(wt)

with step size
η
t=
TSw
w
t
2L|\rho(wt)|

, and starting from

\rho(w0)\ne0

, then
*)\le
\rho(w(1-
t)-\rho(w
\mu
L

)2t

*))
(\rho(w
0)-\rho(w

.

Learning halfspace problem

The problem of learning halfspaces refers to the training of the Perceptron, which is the simplest form of neural network. The optimization problem in this case is

min\tilde{w\in

d}f
R
LH

(\tilde{w})=Ey,x[\phi(zT\tilde{w})]

, where

z=-yx

and

\phi

is an arbitrary loss function.

Suppose that

\phi

is infinitely differentiable and has a bounded derivative. Assume that the objective function

fLH

is

\zeta

-smooth, and that a solution

\alpha*=argmin\alpha||\triangledownf(\alphaw)||2

exists and is bounded such that

-infty<\alpha*<infty

. Also assume

z

is a multivariate normal random variable. With the Gaussian assumption, it can be shown that all critical points lie on the same line, for any choice of loss function

\phi

. Specifically, the gradient of

fLH

could be represented as

\triangledown\tilde{w

}f_(\tilde)=c_1(\tilde)u+c_2(\tilde)S\tilde , where

c1(\tilde{w})=E

(1)
z[\phi
(2)
(z
z[\phi

(zT\tilde{w})](uT\tilde{w})

,

c2(\tilde{w})=E

(2)
z[\phi

(zT\tilde{w})]

, and

\phi(i)

is the

i

-th derivative of

\phi

.

By setting the gradient to 0, it thus follows that the bounded critical points

\tilde{w}*

can be expressed as

\tilde{w}*=

-1
g
*S

u

, where

g*

depends on

\tilde{w}*

and

\phi

. Combining this global property with length-direction decoupling, it could thus be proved that this optimization problem converges linearly.

First, a variation of gradient descent with batch normalization, Gradient Descent in Normalized Parameterization (GDNP), is designed for the objective function

min
w\inRd\backslash\{0\

,\gamma\inR}fLH(w,\gamma)

, such that the direction and length of the weights are updated separately. Denote the stopping criterion of GDNP as

h(wt,\gammat)=E

Tw
t)-E
2
t)

.

Let the step size be

st=s(wt,\gamma

t)=-
||w
3
S
t||
Lgth(wt,\gammat)

.

For each step, if

h(wt,\gammat)\ne0

, then update the direction as

wt+1=wt-st\triangledownwf(wt,\gammat)

.

Then update the length according to

\gammat=Bisection(Ts,f,wt)

, where

Bisection

is the classical bisection algorithm, and

Ts

is the total iterations ran in the bisection step.

Denote the total number of iterations as

Td

, then the final output of GDNP is
\tilde{w}
Td

=

\gamma
Td
w
Td
||w||S
Td

.

The GDNP algorithm thus slightly modifies the batch normalization step for the ease of mathematical analysis.

It can be shown that in GDNP, the partial derivative of

fLH

against the length component converges to zero at a linear rate, such that

(\partial\gammafLH(wt,a

(Ts)
t

)2\le

-Ts
2
(0)
\zeta|b
t
(0)
-a
t
|
\mu2

, where
(0)
a
t

and
0
b
t

are the two starting points of the bisection algorithm on the left and on the right, correspondingly.

Further, for each iteration, the norm of the gradient of

fLH

with respect to

w

converges linearly, such that

||wt||

2||\triangledown
S

fLH(wt,gt)||

2\le
(1-
S-1
\mu
L

)2t

*)
\Phi
0)-\rho

.

Combining these two inequalities, a bound could thus be obtained for the gradient with respect to

\tilde{w}
Td

:

||\triangledown\tilde{w}f(\tilde{w}

Td

)||2\le(1-

\mu
L
2Td
)
*)+
-Ts
2
(0)
\zeta|b
t
(0)
-a
t
|
\mu2
\Phi
0)-\rho

, such that the algorithm is guaranteed to converge linearly.

Although the proof stands on the assumption of Gaussian input, it is also shown in experiments that GDNP could accelerate optimization without this constraint.

Neural networks

Consider a multilayer perceptron (MLP) with one hidden layer and

m

hidden units with mapping from input

x\inRd

to a scalar output described as

Fx(\tilde{W},\Theta)=\sum

m
i=1
T\tilde{w}
\theta
i\phi(x

(i))

, where

\tilde{w}(i)

and

\thetai

are the input and output weights of unit

i

correspondingly, and

\phi

is the activation function and is assumed to be a tanh function.

The input and output weights could then be optimized with

min\tilde{W,\Theta}(fNN(\tilde{W},\Theta)=Ey,x[l(-yFx(\tilde{W},\Theta))])

, where

l

is a loss function,

\tilde{W}=\{\tilde{w}(1),...,\tilde{w}(m)\}

, and

\Theta=\{\theta(1),...,\theta(m)\}

.

Consider fixed

\Theta

and optimizing only

\tilde{W}

, it can be shown that the critical points of

fNN(\tilde{W})

of a particular hidden unit

i

,

\hat{w}(i)

, all align along one line depending on incoming information into the hidden layer, such that

\hat{w}(i)=\hat{c}(i)S-1u

, where

\hat{c}(i)\inR

is a scalar,

i=1,...,m

.

This result could be proved by setting the gradient of

fNN

to zero and solving the system of equations.

Apply the GDNP algorithm to this optimization problem by alternating optimization over the different hidden units. Specifically, for each hidden unit, run GDNP to find the optimal

W

and

\gamma

. With the same choice of stopping criterion and stepsize, it follows that
(i)
||\triangledown
\tilde{w
}f(\tilde_t^)||^2_\le\bigg(1-\frac\bigg)^C(\rho(w_0)-\rho^*)+\frac .

Since the parameters of each hidden unit converge linearly, the whole optimization problem has a linear rate of convergence.

Further reading

Notes and References

  1. Ioffe. Sergey. Szegedy. Christian. Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. 1502.03167. 2015. cs.LG.
  2. Santurkar. Shibani. Tsipras. Dimitris. Ilyas. Andrew. Madry. Aleksander. 2018-05-29. How Does Batch Normalization Help Optimization?. 1805.11604. stat.ML.
  3. 1902.08129. cs.NE. Yang. Greg. Pennington. Jeffrey. Rao. Vinay. Sohl-Dickstein. Jascha. Schoenholz. Samuel S.. A Mean Field Theory of Batch Normalization. 2019.
  4. Kohler. Jonas. Daneshmand. Hadi. Lucchi. Aurelien. Zhou. Ming. Neymeyr. Klaus. Hofmann. Thomas. 2018-05-27. Exponential convergence rates for Batch Normalization: The power of length-direction decoupling in non-convex optimization. 1805.10694. stat.ML.
  5. Simonyan . Karen . Andrew . Zisserman . Very Deep Convolution Networks for Large Scale Image Recognition . 2014 . cs.CV . 1409.1556.
  6. Ba, J., Kiros, J.R., & Hinton, G.E. (2016). Layer Normalization. ArXiv, abs/1607.06450.
  7. Knyazev. Neymeyr. 2003. A geometric theory for preconditioned inverse iteration III: A short and sharp convergence estimate for generalized eigenvalue problems. Linear Algebra and Its Applications. 358. 1–3. 95–114. 10.1016/S0024-3795(01)00461-X. free.