Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Keep every layer’s inputs on a standard scale
In a deep network, each layer’s inputs are the previous layer’s outputs, and as training changes the earlier weights those inputs can drift to very large or very small values. Batch normalisation rescales each feature, inside every mini-batch, to mean 0 and spread 1, then lets the network learn its own preferred scale and offset. Every layer then sees well-scaled inputs, and training can move much faster.
| per-example mean / sd after normalising | 0.55/0.56 0.44/0.57 -0.97/0.62 -0.03/0.57 0.39/1.60 -0.38/0.60 |
| per-channel mean / sd after normalising | -0.00/1.00 0.00/1.00 0.00/1.00 -0.00/1.00 -0.00/1.00 0.00/1.00 0.00/1.00 -0.00/1.00 |
BatchNorm made very deep convolutional networks such as ResNets practical, and it is still in most vision backbones. It has sharp edges that cause real bugs: behaviour that differs between training and inference, failure with tiny batches, and leakage between examples in a batch.
- 1. BatchNorm standardises each feature using…
- 2. After standardising, BatchNorm applies…
- 3. BatchNorm mainly allows…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Training: for a batch of values x₁..x_m of one feature:
- μ = mean(x), σ² = var(x);
- x̂ᵢ = (xᵢ − μ)/√(σ² + ε);
- yᵢ = γx̂ᵢ + β.
- Example: values 1 and 3 give μ = 2, σ = 1, x̂ = (−1, 1).
- Convolutions: one μ, σ² per channel, pooled over the batch and all spatial positions.
- Running statistics: r ← (1 − m)r + m·(batch value), with m = 0.1 in PyTorch. From r = 0, a batch mean of 1 gives 0.1.
- Inference: y = γ(x − r_μ)/√(r_σ² + ε) + β. This is a fixed affine map and can be folded into the preceding linear layer: w’ = w·γ/√(r_σ² + ε), b’ = (b − r_μ)·γ/√(r_σ² + ε) + β.
- Batch noise: a batch mean of m values with spread σ has standard deviation σ/√m: 0.5σ for m = 4, 0.125σ for m = 64.
- 1. x̂ for values 1 and 3:
- 2. Running mean from 0 after one batch mean of 1 (m = 0.1):
- 3. SD of a batch mean of 4 values with σ = 1:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.BatchNorm in code
Exercise 1.
- The forward pass gave each feature mean 0 and standard deviation 1 over the batch.
- The analytic input gradient matched finite differences to 3 × 10⁻⁹ and summed to zero over the batch.
- The output was unchanged when the input was scaled by 5 and shifted by 7.
Practice.
- Place BatchNorm after the convolution or linear layer and before the activation (
Conv → BN → ReLU). The preceding layer’s bias is redundant. - Always switch modes:
model.train()uses batch statistics,model.eval()uses running averages. Forgettingeval()makes predictions depend on the other examples in the batch. - Do not apply weight decay to γ and β.
- For batches below about 8–16 per device, use synchronised BatchNorm across devices, or switch to GroupNorm or LayerNorm (E5-2, E5-4).
- Fold BN into the preceding layer for deployment on embedded hardware.
block = torch.nn.Sequential(torch.nn.Conv2d(64, 64, 3, padding=1, bias=False), torch.nn.BatchNorm2d(64), torch.nn.ReLU())- 1. The bias of a conv layer followed by BN is…
- 2. BatchNorm's input gradient sums over the batch to…
- 3. For tiny per-device batches, use…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
The original explanation. Ioffe and Szegedy (2015) argued that BatchNorm reduces “internal covariate shift”, the change in each layer’s input distribution as earlier layers train. Santurkar et al. (2018) showed that injecting noise to create covariate shift after BN barely hurts, while BN still helps, so this is not the main mechanism.
Smoother optimisation. Santurkar et al. found that BN makes the loss and its gradients more Lipschitz (smoother), so larger steps are safe. Because the output is invariant to the scale of the preceding weights, the gradient is orthogonal to the weights and its size scales as 1/‖w‖. This automatically damps steps when weights grow, an adaptive effective learning rate (Arora, Li and Lyu, 2019).
Noise as regularisation. Each example’s normalisation depends on the other examples in its batch, which adds noise like dropout’s. This regularises with moderate batches and harms with tiny ones.
Train–test discrepancy. Training uses batch statistics; inference uses running averages. If the two differ, as with small or unrepresentative batches, test behaviour differs from training. Exercise 2’s batch-4 failure is a strong case of this. Batch renormalisation (Ioffe, 2017) reduces it.
- 1. Santurkar et al. found BN's main benefit is…
- 2. Scale invariance makes the gradient size scale as…
- 3. Batch renormalisation addresses…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Learning rates and batch sizes
History.
- S. Ioffe and C. Szegedy (2015) introduced BatchNorm.
- K. He et al. (2016) built ResNets around it.
- S. Ioffe (2017) proposed batch renormalisation.
- S. Santurkar, D. Tsipras, A. Ilyas and A. Mądry (2018) challenged the covariate-shift explanation.
- Y. Wu and K. He (2018) proposed GroupNorm for small batches.
Exercise 2. An 8-layer ReLU MLP (width 32) on three spirals, trained by SGD with momentum for 500 steps; test accuracy per seed (two seeds), with running statistics at test time.
| network | η = 0.01 | η = 0.1 | η = 0.3 | η = 1 |
|---|---|---|---|---|
| plain | 0.997, 1.000 | 0.340, 0.331 | 0.340, 0.326 | diverged |
| BatchNorm | 0.998, 0.999 | 0.998, 0.995 | 0.996, 0.989 | 0.317, 0.342 |
| network (η = 0.03 with BN, 0.01 without) | batch 4 | batch 32 |
|---|---|---|
| plain | 0.966, 0.757 | 0.997, 1.000 |
| BatchNorm | 0.592, 0.584 | 0.999, 1.000 |
- BatchNorm widened the usable learning-rate range from 0.01 to 0.01–0.3; the plain network stalled at chance at 0.1 and diverged at 1.
- With batches of 4, BatchNorm did badly: about 0.59 against 0.86 for the plain network. These small-batch runs are sensitive to rounding: in the browser BatchNorm reached 0.74 and 0.62, still well below the plain network. Statistics from 4 examples are noisy, and the running averages used at test time then differ from what training saw.
- Lesson: BatchNorm is a powerful optimisation aid at moderate batch sizes and a liability at tiny ones.
BatchNorm in AI, robotics and aerospace
- Vision backbones (ResNet, EfficientNet, many detectors) use BatchNorm and fold it into convolutions for deployment on embedded GPUs.
- Small-batch regimes, such as high-resolution segmentation or 3-D perception, switch to GroupNorm or synchronised BN.
- Online or reinforcement learning, where batches are correlated, often avoids BatchNorm because its statistics are biased.
References
- S. Ioffe, C. Szegedy (2015). “Batch normalization: accelerating deep network training by reducing internal covariate shift.” Proc. ICML.
- S. Santurkar, D. Tsipras, A. Ilyas, A. Mądry (2018). “How does batch normalization help optimization?” Advances in NeurIPS.
- S. Ioffe (2017). “Batch renormalization: towards reducing minibatch dependence in batch-normalized models.” Advances in NeurIPS.
- S. Arora, Z. Li, K. Lyu (2019). “Theoretical analysis of auto rate-tuning by batch normalization.” Proc. ICLR.
- Y. Wu, K. He (2018). “Group normalization.” Proc. ECCV.