Normalisation layers

BatchNorm, LayerNorm, InstanceNorm, GroupNorm and RMSNorm all compute the same thing — they differ only in which numbers they average.

What you'll learn
  • Name the axes each normalisation layer averages over
  • Explain why BatchNorm struggles with small batches and the others do not
  • Account for the std of 1.016 the notebooks print after normalising

Deep networks train faster when every layer sees inputs of a steady scale. A normalisation layer forces that: subtract the mean, divide by the standard deviation, then let the network rescale with learned parameters \(\gamma\) and \(\beta\):

\[\hat{x} = \frac{x - \mu}{\sqrt{\sigma^2 + \varepsilon}}, \qquad y = \gamma\,\hat{x} + \beta\]

Every variant uses this formula. The only question is which numbers share one \(\mu\) and \(\sigma\) — for a batch of shape \((N, C, H, W)\), which axes are averaged.

A batch of three images, each with four channels of 3×3 pixels. The orange cubes share one mean and variance; the animation steps through each group in turn. Switch the layer to see the groups change shape.
Layer Averages over One statistic per
BatchNorm2d(C) N, H, W channel
LayerNorm([C, H, W]) C, H, W sample
InstanceNorm2d(C) H, W sample and channel
GroupNorm(G, C) C/G, H, W sample and group
RMSNorm C, H, W (no mean) sample

BatchNorm is the odd one out: its statistics mix different images in the batch. That works well with large batches, but with a batch of two the mean is a very noisy estimate, and at inference time — one image at a time — it must fall back on running averages collected during training. The other four normalise each sample on its own, so batch size does not matter. That is why transformers use LayerNorm or RMSNorm, and why GroupNorm exists for detection and segmentation models that only fit a few images in memory.

RMSNorm

The repository’s RMSNorm skips the mean entirely:

rms = torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + self.eps)
return self.scale * (x / rms)

It is applied to input.view(N, -1), so dim=1 covers all \(C \cdot H \cdot W\) features of a sample — the same axes as LayerNorm. Skipping the mean saves a reduction and, in practice, loses little; LLaMA-style language models use it in every block. (PyTorch 2.4 and later ship nn.RMSNorm built in.)

Why the std is not exactly 1

The notebooks print the standard deviation after normalising, and it comes out slightly above 1 — about 1.016 for BatchNorm on 2 × 4 × 4 = 32 values per channel, and about 1.019 for LayerNorm on 3 × 3 × 3 = 27 values per sample. Nothing is wrong. The layers divide by the population variance (divide by \(n\)), while tensor.std() reports the sample std (divide by \(n - 1\)). The ratio is \(\sqrt{n/(n-1)}\): \(\sqrt{32/31} = 1.016\) and \(\sqrt{27/26} = 1.019\), exactly the printed values. tensor.std(unbiased=False) prints 1.000.

Try it yourself. In batch_norm.ipynb, run the layer twice: once in train() mode and once after batch_norm.eval(). The outputs differ — explain why using batch_norm.running_mean. Then replace the layer with nn.GroupNorm(1, C) and nn.GroupNorm(C, C), and show that they match LayerNorm and InstanceNorm respectively (up to the affine parameters).

View source on GitHub →