Computer Vision Lab Algorithms Batch normalisation

Batch normalisation

Batch normalization

Normalising a layer's outputs with the batch mean and variance, then rescaling them with learned coefficients.

Concept and explanationConvolution and neural networksNeural networks

This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.

Mathematical formula

\hat{x} = \frac{x - \mu_B}{\sqrt{\sigma_B^{2} + \epsilon}},\quad y = \gamma\hat{x} + \beta

Explanation

It speeds up training and allows higher learning rates. At inference, accumulated statistics are used, and it can be folded into the preceding convolution.

Detailed explanation

Intuition

During training, the distributions of each layer's activations change constantly, so learning becomes sensitive to the learning rate and the initialisation. Batch normalisation normalises each channel to zero mean and unit variance using the statistics of the current batch, then rescales it with two learnable parameters \gamma,\beta. The result is faster, more stable training with higher learning rates, plus a mild regularising effect. The “internal covariate shift” explanation proposed by the original paper has been debated in later work, but the practical benefit is well established.

How it works step by step

  1. For each channel: compute the mean and variance over the batch and spatial dimensions (N,H,W).
  2. Normalise, then rescale and shift.
  3. Update a moving average of the statistics for use at inference.
  4. At inference: use the fixed moving statistics, and the whole layer can be folded into the preceding convolution.

Mathematical formulation

\mu_B=\frac1m\sum_i x_i,\quad \sigma_B^2=\frac1m\sum_i (x_i-\mu_B)^2,\quad \hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^{2} + \epsilon}},\quad y_i = \gamma\,\hat{x}_i + \beta

Computational complexity

O(NCHW), and only two parameters per channel.

Advantages

  • Faster convergence and less sensitivity to initialisation.
  • Allows higher learning rates.
  • Free at inference after folding into the convolution.

Limitations

  • It depends on the batch size: with small batches (2–4, common in 3D medical segmentation) the statistics become noisy; use GroupNorm or InstanceNorm.
  • Different behaviour between training and inference; forgetting model.eval() is a very common mistake.
  • Problems with distributed training without synchronisation (SyncBN).

When do you use it?

In most convolutional networks. Transformers use LayerNorm because it does not depend on the batch.

Code example (PyTorch)

import torch.nn as nn
block = nn.Sequential(nn.Conv2d(64, 64, 3, padding=1, bias=False), nn.BatchNorm2d(64), nn.ReLU())
model.eval()   # required before inference

Applications

Almost all modern networks.

Limitations

It is affected by small batch sizes.

References

  1. S. Ioffe, C. Szegedy (2015).Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. arXiv:1502.03167 (ICML 2015). doi:10.48550/arXiv.1502.03167

Related models

Related lessons

Try it in convolution and neural networks