Foundations to Frontier
Tests
E. Deep Learning › E5 Normalization Layers (one page each)

Batch Normalization

Batch normalisation standardises each feature over the current mini-batch, (x − batch mean)/batch standard deviation, then applies a learned scale and shift. It lets deep networks train with much larger learning rates and less careful initialisation, and in convolutional networks it is applied per channel. At test time it uses running averages of the statistics. This page covers the forward and backward pass, training versus inference behaviour, why it helps (smoother loss landscapes rather than the originally proposed 'internal covariate shift'), and its weaknesses with small or non-independent batches. In the exercises you implement and gradient-check BatchNorm, then train an 8-layer network and find BatchNorm made learning rates up to 0.3 usable where the plain network stalled above 0.01, but failed with batches of 4.

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.

Normalisation layers: which values share a mean and variance?
channels →ex 1ex 2ex 3ex 4ex 5ex 6
Click a cell. Highlighted cells (16 values each) are pooled to compute the mean and variance used to normalise it.
per-example mean / sd after normalising0.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 noise: with batch size 6, a channel's batch mean is off from its population mean by 0.288 on average.
BatchNorm makes every channel have mean 0 and sd 1 across the batch, but each example's own statistics still vary, and its statistics depend on which other examples are in the batch: shrink N to 1 or 2 and watch the noise grow. LayerNorm, InstanceNorm and GroupNorm compute everything within one example, so they work the same at any batch size and at test time; they differ in which channels they pool.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. BatchNorm standardises each feature using…
  2. 2. After standardising, BatchNorm applies…
  3. 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.
📝 Quick check (Level 1)
0/3 answered
  1. 1. x̂ for values 1 and 3:
  2. 2. Running mean from 0 after one batch mean of 1 (m = 0.1):
  3. 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. Forgetting eval() 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())
🐍 Exercise 1: BatchNorm forward and backward
Implement BatchNorm's forward and backward passes, check the gradient against finite differences, and check invariance to scaling and shifting the input.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The bias of a conv layer followed by BN is…
  2. 2. BatchNorm's input gradient sums over the batch to…
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Santurkar et al. found BN's main benefit is…
  2. 2. Scale invariance makes the gradient size scale as…
  3. 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.
🐍 Exercise 2 (research): learning rates and batch sizes
Train a deep MLP with and without BatchNorm over learning rates and batch sizes, and compare test accuracy.

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

  1. S. Ioffe, C. Szegedy (2015). “Batch normalization: accelerating deep network training by reducing internal covariate shift.” Proc. ICML.
  2. S. Santurkar, D. Tsipras, A. Ilyas, A. Mądry (2018). “How does batch normalization help optimization?” Advances in NeurIPS.
  3. S. Ioffe (2017). “Batch renormalization: towards reducing minibatch dependence in batch-normalized models.” Advances in NeurIPS.
  4. S. Arora, Z. Li, K. Lyu (2019). “Theoretical analysis of auto rate-tuning by batch normalization.” Proc. ICLR.
  5. Y. Wu, K. He (2018). “Group normalization.” Proc. ECCV.