3.2.8Training Deep Networks

Batch normalization

2,005 words9 min readdifficulty · medium6 backlinks

WHAT is Batch Normalization?


WHY do we need it? (Steel-manning the problem)

Consider a deep network. Layer \ell receives inputs from layer 1\ell-1. Gradient descent updates all layers simultaneously using gradients computed as if the other layers were frozen. But they're not frozen — so after one step, the inputs feeding layer \ell have a different mean and spread.

WHY this hurts:

  • If inputs to a sigmoid/tanh drift to large magnitude, activations saturate → gradients vanish.
  • Different scales across features make the loss surface ill-conditioned (long narrow valleys), forcing tiny learning rates.

HOW BN helps: by re-centering and re-scaling each activation every step, the layer above always sees a well-behaved distribution, so we can use larger learning rates and worry less about initialization.


HOW: deriving BN from first principles

We want each activation, per mini-batch, to have mean 0 and variance 1. So compute the statistics of the mini-batch.

Let the mini-batch be B={x1,,xm}\mathcal{B} = \{x_1, \dots, x_m\} (one scalar activation, mm examples).

Step 1 — batch mean. Why? We need the center to subtract. μB=1mi=1mxi\mu_\mathcal{B} = \frac{1}{m}\sum_{i=1}^m x_i

Step 2 — batch variance. Why? We need the spread to divide by. σB2=1mi=1m(xiμB)2\sigma_\mathcal{B}^2 = \frac{1}{m}\sum_{i=1}^m (x_i - \mu_\mathcal{B})^2

Step 3 — normalize. Why the ϵ\epsilon? To avoid dividing by zero when a batch happens to be constant; it keeps things numerically stable. x^i=xiμBσB2+ϵ\hat{x}_i = \frac{x_i - \mu_\mathcal{B}}{\sqrt{\sigma_\mathcal{B}^2 + \epsilon}} Now x^\hat{x} has (approximately) mean 0, variance 1.

Step 4 — scale and shift. Why undo what we just did? Forcing mean 0/var 1 may destroy useful representational power (e.g. a sigmoid needs inputs away from 0 to be nonlinear). So we give the network the freedom to learn the best mean/scale via two learnable parameters: yi=γx^i+βy_i = \gamma\,\hat{x}_i + \beta

Figure — Batch normalization

Train vs. Test — a subtle but crucial point


Backpropagation through BN (why it still trains)

BN is fully differentiable, so gradients flow through μB\mu_\mathcal{B} and σB2\sigma^2_\mathcal{B} too. The parameter gradients are simple: Lγ=iLyix^i,Lβ=iLyi\frac{\partial L}{\partial \gamma}=\sum_i \frac{\partial L}{\partial y_i}\hat{x}_i,\qquad \frac{\partial L}{\partial \beta}=\sum_i \frac{\partial L}{\partial y_i} Why these forms? Since yi=γx^i+βy_i=\gamma\hat x_i+\beta, differentiate: yi/γ=x^i\partial y_i/\partial\gamma=\hat x_i and yi/β=1\partial y_i/\partial\beta=1, then sum over the batch (both params are shared across all mm examples).


Worked Examples


Common Mistakes (Steel-manned)


Benefits summary (the 80/20)


Recall Feynman: explain to a 12-year-old

Imagine a relay race where each runner passes a baton, but the height they hold it at keeps changing every round, so the next runner keeps fumbling. Batch Norm is a rule: "before passing, always hold the baton at the same standard height." Now everyone knows what to expect and the race speeds up. And if a runner really needs a different height, we let them adjust it on purpose (that's the γ,β\gamma,\beta dials) — but from a known starting point, not chaos.


Flashcards

What problem does Batch Normalization aim to reduce?
Internal covariate shift — the drift in the distribution of a layer's inputs as earlier layers update during training.
Write the 4 forward steps of BN.
Compute batch mean μB\mu_\mathcal{B}; batch variance σB2\sigma^2_\mathcal{B}; normalize x^=(xμ)/σ2+ϵ\hat x=(x-\mu)/\sqrt{\sigma^2+\epsilon}; scale+shift y=γx^+βy=\gamma\hat x+\beta.
Why is ϵ\epsilon added under the square root?
Numerical stability — prevents division by zero when the batch variance is (near) zero.
What is the purpose of the learnable γ\gamma and β\beta?
To restore representational power — the network can re-learn any scale/shift, even the identity, so normalization never limits it.
What statistics does BN use at inference time and why?
Running (EMA) estimates of population mean/variance, because a single test example has no meaningful batch statistics; makes output deterministic.
Why can you drop the bias in the layer before BN?
BN subtracts the mean, cancelling any constant bias; the β\beta parameter already provides the shift.
Why does BN allow higher learning rates?
It keeps activations well-conditioned (stable scale), smoothing the loss landscape and preventing saturation/exploding activations.
Why does BN behave badly with very small batch sizes?
Mean/variance estimates from few samples are noisy/unreliable; use LayerNorm or GroupNorm instead.
Gradient of loss w.r.t. γ\gamma?
iLyix^i\sum_i \frac{\partial L}{\partial y_i}\hat x_i.
Gradient of loss w.r.t. β\beta?
iLyi\sum_i \frac{\partial L}{\partial y_i}.

Connections

Concept Map

causes

leads to

leads to

motivates

computes

computes

feeds

feeds

gives zero mean unit variance

restores representational power

reduces

Internal covariate shift

Layers updated simultaneously

Saturation and vanishing gradients

Ill-conditioned loss surface

Batch Normalization layer

Batch mean

Batch variance

Normalize with epsilon

Scale gamma and shift beta

Larger learning rates and stable training

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, deep network mein har layer apne se pehle wali layers ka output leti hai. Training ke dauran jab saari layers ke weights update hote hain, toh kisi bhi layer ke input ki distribution (mean aur spread) badalti rehti hai — isko internal covariate shift kehte hain. Matlab har layer ek hilte hue target ko chase kar rahi hai, jisse training slow aur unstable ho jaati hai, learning rate chhota rakhna padta hai.

Batch Normalization ka jugaad simple hai: har mini-batch ke liye us activation ka mean nikaalo, variance nikaalo, phir normalize kar do taaki mean 0 aur variance 1 ho jaaye. Lekin sirf normalize karne se network ki power ghat sakti hai, isliye do learnable parameters — γ\gamma (scale) aur β\beta (shift) — de dete hain, taaki network apni pasand ki range khud seekh le. Chaaho toh network identity bhi wapas la sakta hai, so kuch loss nahi hota.

Ek important baat: training time pe batch ke apne stats use hote hain, but test time pe ek hi example aa sakta hai, toh hum training ke dauran ka running average (mean/variance) use karte hain. Isse test output deterministic ho jaata hai. Aur haan — BN se pehle wali layer ka bias hata do, kyunki BN mean subtract karke usko cancel kar deta hai, β\beta hi uska kaam kar deta hai.

Kyun important hai? Kyunki BN se tum bade learning rate use kar sakte ho, training fast aur stable hoti hai, initialization pe kam depend karti hai, aur thoda regularization bhi free mein milta hai. Bas dhyan rakho — bahut chhote batch size (jaise 2) pe BN ke stats noisy ho jaate hain, tab LayerNorm ya GroupNorm behtar hai.

Go deeper — visual, from zero

Test yourself — Training Deep Networks

Connections