Simple Algorithm
Machine Learning · Convergence · 26 min

Normalization Layers

BatchNorm, LayerNorm and why they help optimization.

Batch normalization was introduced to cure "internal covariate shift". Three years later, a network with that shift injected on purpose trained just as well, and nobody stopped using the layer. This lesson derives what normalization actually does to gradients, to the learning rate and to weight decay, compares the family of normalizers by the symmetries they create, and looks at networks that train well with no normalization at all.

What you’ll learn

  • Derive the Jacobian of batch normalization and show that its backward pass projects out the batch mean and the normalized direction.
  • Prove that gradient descent with weight decay on scale-invariant weights is gradient descent without decay under an exponentially growing learning rate, and derive the equilibrium angular update 2ηλ\sqrt{2\eta\lambda}.
  • Weigh the competing explanations for why normalization helps against the experiments that test them.
  • Compare BatchNorm, LayerNorm, GroupNorm, RMSNorm and weight normalization by the statistics they use and the invariances they create, and explain how normalizer-free networks replace them.

The normalization map and its backward pass

Fix one unit and a mini-batch of mm examples, and collect its pre-activations in z∈Rm\mathbf z\in\R^m. Batch normalization outputs y=γz^+β1\mathbf y=\gamma\hat{\mathbf z}+\beta\mathbf 1 with z^=(z−μ1)/σ\hat{\mathbf z}=(\mathbf z-\mu\mathbf 1)/\sigma, μ=1m1⊤z\mu=\frac1m\mathbf 1^\top\mathbf z and σ2=1m∥z−μ1∥2\sigma^2=\frac1m\norm{\mathbf z-\mu\mathbf 1}^2 (we drop the small ϵ\epsilon). Layer normalization applies the same map to the vector of one example's features. Since μ\mu and σ\sigma depend on every coordinate, each output depends on all inputs, and so does the gradient.

DerivationThe Jacobian of standardization
  1. c=Pz,P=I−1m11⊤,z^=m c∥c∥\mathbf c=P\mathbf z,\qquad P=I-\tfrac1m\mathbf 1\mathbf 1^\top,\qquad\hat{\mathbf z}=\sqrt m\,\frac{\mathbf c}{\norm{\mathbf c}}

    Centering is the orthogonal projection PP onto the complement of 1\mathbf 1. With c=Pz\mathbf c=P\mathbf z we have σ=∥c∥/m\sigma=\norm{\mathbf c}/\sqrt m, so the normalized vector is a rescaled unit vector:

  2. ∂z^∂c=1σ(I−1mz^z^⊤)\frac{\partial\hat{\mathbf z}}{\partial\mathbf c}=\frac1\sigma\Big(I-\frac1m\hat{\mathbf z}\hat{\mathbf z}^\top\Big)

    The derivative of c↦c/∥c∥\mathbf c\mapsto\mathbf c/\norm{\mathbf c} is (I−uu⊤)/∥c∥(I-\mathbf u\mathbf u^\top)/\norm{\mathbf c} with u=c/∥c∥=z^/m\mathbf u=\mathbf c/\norm{\mathbf c}=\hat{\mathbf z}/\sqrt m. Multiplying by m\sqrt m and using ∥c∥=m σ\norm{\mathbf c}=\sqrt m\,\sigma:

  3. ∂z^∂z=1σ(I−1m11⊤−1mz^z^⊤)\frac{\partial\hat{\mathbf z}}{\partial\mathbf z}=\frac1\sigma\Big(I-\frac1m\mathbf 1\mathbf 1^\top-\frac1m\hat{\mathbf z}\hat{\mathbf z}^\top\Big)

    Chain with ∂c/∂z=P\partial\mathbf c/\partial\mathbf z=P. The normalized vector has mean zero, so Pz^=z^P\hat{\mathbf z}=\hat{\mathbf z} and the two projections merge into one symmetric matrix:

  4. ∂L∂z=γσ(g−gˉ 1−z^⊤gm z^),gˉ=1m1⊤g\frac{\partial L}{\partial\mathbf z}=\frac\gamma\sigma\Big(\mathbf g-\bar g\,\mathbf 1-\frac{\hat{\mathbf z}^\top\mathbf g}{m}\,\hat{\mathbf z}\Big),\qquad\bar g=\frac1m\mathbf 1^\top\mathbf g

    Backpropagate an upstream gradient g=∂L/∂y\mathbf g=\partial L/\partial\mathbf y. The vectors 1\mathbf 1 and z^\hat{\mathbf z} are orthogonal and both have squared norm mm, so the bracket is the orthogonal projection onto the complement of their span:

Two facts follow. First, the gradient that reaches z\mathbf z has zero mean over the batch and is uncorrelated with z^\hat{\mathbf z}: the parts of g\mathbf g that would only shift or rescale the whole batch are removed, because shifting or rescaling z\mathbf z cannot change the output. Second, ∥∂L/∂z∥≤(∣γ∣/σ)∥g∥\norm{\partial L/\partial\mathbf z}\le(\abs\gamma/\sigma)\norm{\mathbf g}: if earlier weights grow and inflate σ\sigma, the gradient shrinks in proportion, a built-in brake.

Santurkar, Tsipras, Ilyas & Madry (2018) turned this projection into bounds on the gradient norm and on the curvature along the gradient of a batch-normalized network, compared with the same network without the layer, and read them as a smoother loss landscape that tolerates larger steps. How much smoother depends on how much of g\mathbf g lies in the removed directions and on γ/σ\gamma/\sigma, so the bounds do not guarantee an improvement in every case.

Predict before you look

A batch of m=4m=4 has normalized pre-activations z^=(1,1,−1,−1)\hat{\mathbf z}=(1,1,-1,-1), upstream gradient g=(3,1,1,1)\mathbf g=(3,1,1,1), and γ/σ=2\gamma/\sigma=2. What is the norm of ∂L/∂z\partial L/\partial\mathbf z?

Show the answer

2.83

gˉ=1.5\bar g=1.5, so g−gˉ1=(1.5,−0.5,−0.5,−0.5)\mathbf g-\bar g\mathbf 1=(1.5,-0.5,-0.5,-0.5). Next, z^⊤g/m=(3+1−1−1)/4=0.5\hat{\mathbf z}^\top\mathbf g/m=(3+1-1-1)/4=0.5; subtracting 0.5 z^0.5\,\hat{\mathbf z} leaves (1,−1,0,0)(1,-1,0,0). Times γ/σ=2\gamma/\sigma=2: (2,−2,0,0)(2,-2,0,0), with norm 22≈2.832\sqrt2\approx2.83, against ∥g∥⋅2≈6.93\norm{\mathbf g}\cdot2\approx6.93 before the projection. A finite-difference check of the layer gives the same vector.

Why does it help? The candidate explanations

The input layer is where the story starts. LeCun, Bottou, Orr & Müller (1998) observed that the Hessian of a linear unit is proportional to the second-moment matrix of its inputs (with a constant 1 standing in for the bias), so centering, scaling and decorrelating the inputs bring it close to a multiple of the identity. The widget fits a line y^=wx+b\hat y=wx+b by full-batch gradient descent, on standardized inputs ("round") or on inputs with mean 0.905 and standard deviation 0.43 ("elongated", condition number 20).

ExplanationClaimStatus
Internal covariate shift (Ioffe & Szegedy, 2015)stabilizing layer-input distributions speeds trainingthe noise-injection experiment undercuts it
Smoother landscape (Santurkar et al., 2018)the projection above improves Lipschitz and smoothness constantsbounds hold under conditions; widely cited, not a full account
Larger learning rates (Bjorck, Gomes, Selman & Weinberger, 2018)normalization mainly enables larger steps, which speed convergence and may help generalizationsupported empirically, though De & Smith (2020) found larger rates help mainly at large batch sizes
Automatic rate tuning (Arora, Li & Lyu, 2019)with the rate of scale-invariant weights fixed at any constant, gradient descent still reaches a stationary point at rate T−1/2T^{-1/2}proved; the norm of the weights adapts the step
Residual branches start small (De & Smith, 2020)at initialization normalization shrinks each residual branch by a factor of order depth\sqrt{\text{depth}}, so blocks start near the identityproved at initialization; reproduced without normalization by "SkipInit"
Sharpness reduction (Lyu, Li & Arora, 2022)with weight decay, gradient descent on scale-invariant losses follows a flow that reduces a scale-invariant sharpnessproved for a broad class of networks, near the edge of stability
One caution in the other direction: in deep fully connected networks without skip connections, mean-field theory shows that batch normalization itself makes gradients explode exponentially with depth at initialization, whatever the weight variance or activation function (Yang, Pennington, Rao, Sohl-Dickstein & Schoenholz, 2019).
Check your understanding

What did the noise-injection experiment of Santurkar et al. (2018) establish?

  1. Batch normalization works by keeping each layer's input distribution stable
  2. Networks with deliberately unstable layer-input distributions after normalization trained about as well as ordinary batch-normalized ones
  3. Noise after normalization layers makes training diverge
  4. Batch normalization only helps networks without residual connections
Show the answer

B. Networks with deliberately unstable layer-input distributions after normalization trained about as well as ordinary batch-normalized ones

The injected noise changed every unit's input distribution at every step, more than in a network without normalization, yet training was almost unaffected. Whatever normalization does, it is not mainly reducing covariate shift.

Scale invariance meets weight decay

DerivationWeight decay is an exponentially growing learning rate (Li & Arora, 2020)
  1. wt+1=ρ wt−η ∇L(wt),ρ=1−ηλ∈(0,1)\mathbf w_{t+1}=\rho\,\mathbf w_t-\eta\,\nabla L(\mathbf w_t),\qquad\rho=1-\eta\lambda\in(0,1)

    Gradient descent with weight decay λ\lambda at a constant rate η\eta multiplies the weights by ρ=1−ηλ\rho=1-\eta\lambda before each gradient step:

  2. w~t+1=ρ−t−1(ρ wt−η∇L(wt))=w~t−η ρ−t−1 ∇L(ρtw~t)\tilde{\mathbf w}_{t+1}=\rho^{-t-1}\big(\rho\,\mathbf w_t-\eta\nabla L(\mathbf w_t)\big)=\tilde{\mathbf w}_t-\eta\,\rho^{-t-1}\,\nabla L(\rho^t\tilde{\mathbf w}_t)

    Undo the shrinking: define w~t=ρ−twt\tilde{\mathbf w}_t=\rho^{-t}\mathbf w_t, a positive multiple of wt\mathbf w_t, so the network computes exactly the same function. Its update is

  3. w~t+1=w~t−ηt ∇L(w~t),ηt=η (1−ηλ)−2t−1\tilde{\mathbf w}_{t+1}=\tilde{\mathbf w}_t-\eta_t\,\nabla L(\tilde{\mathbf w}_t),\qquad\eta_t=\eta\,(1-\eta\lambda)^{-2t-1}

    Scale invariance gives ∇L(ρtw~t)=ρ−t∇L(w~t)\nabla L(\rho^t\tilde{\mathbf w}_t)=\rho^{-t}\nabla L(\tilde{\mathbf w}_t). So the sequence w~t\tilde{\mathbf w}_t is plain gradient descent, with no decay, and a learning rate that grows by the factor ρ−2\rho^{-2} every step:

  4. decay λ at rate η≡no decay at rate η ρ−(2t+1)\text{decay }\lambda\text{ at rate }\eta\quad\equiv\quad\text{no decay at rate }\eta\,\rho^{-(2t+1)}

    The two runs produce the same predictions at every step. Li & Arora (2020) prove the analogue with momentum, where the growth factor solves a quadratic equation, and trained standard batch-normalized networks with an exponentially increasing learning rate and no weight decay. On such weights, decay does not shrink the function class (only the direction w^\hat{\mathbf w} matters); it changes the effective step size.

DerivationThe equilibrium: angular steps of size √(2ηλ)
  1. ∥wt+1∥2=ρ2∥wt∥2+η2∥gt∥2,∥gt∥=Gt∥wt∥,Gt=∥∇L(w^t)∥\norm{\mathbf w_{t+1}}^2=\rho^2\norm{\mathbf w_t}^2+\eta^2\norm{\mathbf g_t}^2,\qquad\norm{\mathbf g_t}=\frac{G_t}{\norm{\mathbf w_t}},\quad G_t=\norm{\nabla L(\hat{\mathbf w}_t)}

    Because the gradient gt\mathbf g_t is orthogonal to wt\mathbf w_t, Pythagoras gives the norm after one step with decay:

  2. N=ρ2N+η2G2N⟹N=η G1−ρ2N=\rho^2N+\frac{\eta^2G^2}N\quad\Longrightarrow\quad N=\frac{\eta\,G}{\sqrt{1-\rho^2}}

    Decay pulls the norm down and the gradient steps push it up. If GtG_t hovers around a typical value GG, the squared norm N=∥w∥2N=\norm{\mathbf w}^2 settles where the two balance:

  3. Δθ≈1−ρ2=2ηλ−η2λ2≈2ηλ\Delta\theta\approx\sqrt{1-\rho^2}=\sqrt{2\eta\lambda-\eta^2\lambda^2}\approx\sqrt{2\eta\lambda}

    Each step then turns the direction by an angle of about η∥gt∥/∥wt∥=ηG/N\eta\norm{\mathbf g_t}/\norm{\mathbf w_t}=\eta G/N. Substituting NN, the gradient size cancels:

At equilibrium the direction moves at a pace set by the product ηλ\eta\lambda alone, however small the gradients have become: normalization plus decay acts like a constant "angular" learning rate. Linearizing the norm recursion shows the equilibrium is approached on a time scale of order 1/(ηλ)1/(\eta\lambda) steps. Wan, Zhu, Zhang & Sun (2021) proved, under stated assumptions, that the norm and the angular update converge to their equilibrium values at a linear rate, with 2ηλ/(1+β)\sqrt{2\eta\lambda/(1+\beta)} for heavy-ball momentum β\beta, and measured it on ImageNet and COCO training runs. Empirically, Zhang, Wang, Xu & Grosse (2019) identified a larger effective learning rate as one of three distinct mechanisms by which weight decay regularizes networks.

Predict before you look

SGD without momentum, η=0.1\eta=0.1, weight decay λ=5×10−4\lambda=5\times10^{-4}, on weights that feed a normalization layer. At equilibrium, by about how many degrees does the weight direction turn per step?

Show the answer

0.57

2ηλ=10−4=0.01\sqrt{2\eta\lambda}=\sqrt{10^{-4}}=0.01 radian, about 0.57°0.57°. Doubling η\eta and halving λ\lambda leaves it unchanged; quadrupling η\eta alone doubles it. That is why the learning rate and the weight decay of normalized networks are best tuned together.

Check your understanding

Run A uses η=0.2\eta=0.2, λ=10−4\lambda=10^{-4}; run B uses η=0.05\eta=0.05, λ=4×10−4\lambda=4\times10^{-4}. Both train weights feeding normalization layers with plain SGD. How do their equilibrium angular updates compare?

  1. A's is four times B's
  2. B's is four times A's
  3. A's is twice B's
  4. They are about equal
Show the answer

D. They are about equal

Both have ηλ=2×10−5\eta\lambda=2\times10^{-5}, so both settle at 2ηλ≈0.0063\sqrt{2\eta\lambda}\approx0.0063 radian per step. The raw learning rates differ by a factor of 4, but the equilibrium norms differ to compensate.

The normalizer family, and life without it

MethodNormalizes overOutput unchanged whenNotes
BatchNorm (Ioffe & Szegedy, 2015)one unit (or channel) across the batcha unit's pre-activations are shifted or rescaled across the batchexamples interact; running averages at inference
LayerNorm (Ba, Kiros & Hinton, 2016)all units of one examplean example's pre-activation vector is shifted by a constant or rescaledidentical in training and inference
GroupNorm (Wu & He, 2018)groups of channels within one examplea group is shifted or rescaledone group is LayerNorm over channels; one channel per group is instance normalization
RMSNorm (Zhang & Sennrich, 2019)all units of one example, without centering: z/RMS(z)\mathbf z/\mathrm{RMS}(\mathbf z)the vector is rescaled (not shifted)cheaper; widely used in large language models as of the mid-2020s
Weight normalization (Salimans & Kingma, 2016)no data statistics: w=g v/∥v∥\mathbf w=g\,\mathbf v/\norm{\mathbf v}v\mathbf v is rescaleda reparameterization; no batch dependence
Every method makes some weights scale-invariant, so the previous step applies to all of them. They differ in which examples interact, in what they cost, and in whether they also remove a shift.

Where the normalization sits matters. The original transformer applied layer normalization after each residual addition ("Post-LN"). Xiong et al. (2020) showed with mean-field theory that at initialization this gives large expected gradients for the parameters near the output, which makes a learning-rate warmup necessary; placing the normalization inside each residual branch ("Pre-LN") makes the gradients well behaved, and Pre-LN transformers trained comparably without warmup. Many large language models of the mid-2020s use the Pre-LN arrangement, often with RMSNorm.

Training without normalization. Batch dependence complicates small-batch and distributed training, and the train–test switch is a source of bugs. De & Smith (2020) traced BatchNorm's benefit in deep residual networks to the downscaled residual branch and reproduced it with "SkipInit": a learnable scalar at the end of each residual branch, initialized at zero. Brock, De, Smith & Simonyan (2021) built Normalizer-Free ResNets (NFNets) around adaptive gradient clipping, which rescales the gradient GiG_i of each unit ii (a row of a weight matrix WW) whenever it is large relative to that unit's weights:

Gi ← λ max⁡(∥Wi∥F,ϵ)∥Gi∥F Giif∥Gi∥Fmax⁡(∥Wi∥F,ϵ)>λG_i\ \leftarrow\ \lambda\,\frac{\max(\norm{W_i}_F,\epsilon)}{\norm{G_i}_F}\,G_i\qquad\text{if}\quad\frac{\norm{G_i}_F}{\max(\norm{W_i}_F,\epsilon)}>\lambda
A cap on the relative update: with plain gradient descent at learning rate η\eta, one step can change a unit's weights by at most a fraction ηλ\eta\lambda of their norm (here λ\lambda is the clipping threshold, not a weight decay). Brock et al. reported that it overcame the instabilities of earlier normalizer-free networks at large learning rates and with strong augmentation, and that their models matched or beat batch-normalized ones on ImageNet.

Key takeaways

  • Batch normalization's Jacobian is 1σ\frac1\sigma times the projection removing 1\mathbf 1 and z^\hat{\mathbf z}: the backward pass discards gradient components that could only shift or rescale the batch, and shrinks the rest when σ\sigma grows.
  • Deliberately injected covariate shift barely affects batch-normalized training; larger stable learning rates, automatic rate tuning, near-identity residual blocks and sharpness reduction are better-supported explanations, and none is the whole story.
  • On scale-invariant weights, decay λ\lambda at rate η\eta equals no decay at rate η(1−ηλ)−2t−1\eta(1-\eta\lambda)^{-2t-1} (Li & Arora), and at equilibrium the direction turns by about 2ηλ\sqrt{2\eta\lambda} per step: decay sets the effective learning rate.
  • Normalizers differ in which statistics they pool (batch, example, group; centered or not) and in the invariances they create; their placement matters (Pre-LN vs Post-LN), and SkipInit or adaptive gradient clipping can replace them.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In a batch-normalization layer, the gradient ∂L/∂z\partial L/\partial\mathbf z with respect to one unit's pre-activations over the batch is always orthogonal to which vectors?

  1. Only the all-ones vector 1\mathbf 1
  2. The upstream gradient ∂L/∂y\partial L/\partial\mathbf y
  3. Both 1\mathbf 1 and the normalized vector z^\hat{\mathbf z}
  4. None in general
Show the answer

C. Both 1\mathbf 1 and the normalized vector z^\hat{\mathbf z}

The Jacobian is 1σ(I−1m11⊤−1mz^z^⊤)\frac1\sigma(I-\frac1m\mathbf 1\mathbf 1^\top-\frac1m\hat{\mathbf z}\hat{\mathbf z}^\top), the projection onto the complement of span⁡{1,z^}\operatorname{span}\{\mathbf 1,\hat{\mathbf z}\} up to the factor 1/σ1/\sigma. These are exactly the directions along which moving z\mathbf z (a shift or a rescaling) leaves the output unchanged.

Question 2

For weights on which the loss is scale-invariant, gradient descent with weight decay λ\lambda at a constant learning rate η\eta produces the same sequence of functions as which procedure?

  1. Gradient descent without decay, with the learning rate shrinking as η(1−ηλ)t\eta(1-\eta\lambda)^{t}
  2. Gradient descent without decay, with the learning rate growing as η(1−ηλ)−2t−1\eta(1-\eta\lambda)^{-2t-1}
  3. Ridge regression with penalty λ\lambda
  4. Gradient descent without decay at the constant rate η/(1−ηλ)\eta/(1-\eta\lambda)
Show the answer

B. Gradient descent without decay, with the learning rate growing as η(1−ηλ)−2t−1\eta(1-\eta\lambda)^{-2t-1}

Rescaling w~t=(1−ηλ)−twt\tilde{\mathbf w}_t=(1-\eta\lambda)^{-t}\mathbf w_t leaves the function unchanged and turns each decayed step into a plain step with rate η(1−ηλ)−2t−1\eta(1-\eta\lambda)^{-2t-1}, because the gradient of a scale-invariant loss scales inversely with the weights.

Question 3

At the equilibrium of SGD with weight decay on scale-invariant weights, what happens to the angular update if you multiply the learning rate by 4 and keep λ\lambda fixed?

  1. It doubles
  2. It quadruples
  3. It does not change, because the weight norm compensates completely
  4. It halves
Show the answer

A. It doubles

The angular update is about 2ηλ\sqrt{2\eta\lambda}, so a fourfold η\eta doubles it. The norm compensates only partly: it grows by a factor of 2 in squared norm terms (N∝η/ηλ=η/λN\propto\eta/\sqrt{\eta\lambda}=\sqrt{\eta/\lambda}), not enough to cancel a fourfold step.

Question 4

According to Xiong et al. (2020), why do Post-LN transformers need a learning-rate warmup while Pre-LN transformers can do without one?

  1. Pre-LN transformers have fewer parameters
  2. In Post-LN, the parameters closest to the output receive large expected gradients at initialization, so full-size early steps are unstable
  3. Layer normalization does not work at batch size 1
  4. Post-LN computes its statistics over the batch
Show the answer

B. In Post-LN, the parameters closest to the output receive large expected gradients at initialization, so full-size early steps are unstable

Their mean-field analysis shows large gradients near the output at initialization when normalization follows each residual addition. Putting it inside the residual branch keeps the gradients well behaved from the start.

Further reading

  • S. Ioffe & C. Szegedy, "Batch normalization: accelerating deep network training by reducing internal covariate shift," ICML (2015).
  • S. Santurkar, D. Tsipras, A. Ilyas & A. Madry, "How does batch normalization help optimization?," NeurIPS (2018).
  • Z. Li & S. Arora, "An exponential learning rate schedule for deep learning," ICLR (2020).
  • R. Xiong, Y. Yang, D. He, K. Zheng, S. Zheng, C. Xing, H. Zhang, Y. Lan, L. Wang & T.-Y. Liu, "On layer normalization in the transformer architecture," ICML (2020).
  • A. Brock, S. De, S. L. Smith & K. Simonyan, "High-performance large-scale image recognition without normalization," ICML (2021).