TL;DR
Batch normalization takes the values a layer produces for one mini-batch of examples and rescales each feature so that its mean is 0 and its variance is 1. It then applies a learned scale and shift, so the network can still choose whatever spread and centre it actually wants. Deep networks with this step train faster and are less sensitive to the initial weights and the learning rate.
Where you'll see it: between the linear layers and the activation functions of most convolutional image classifiers, such as the ResNet family, and in many other networks that stack many layers.
Intuition
A thermostat for each feature
Picture a deep network as a long chain of layers. Each layer is trying to learn a useful mapping, but the layer before it keeps changing its outputs as training goes on. So every layer sees inputs whose distribution is a moving target. Later layers spend effort chasing that drift instead of learning the task.
Batch normalization works like a thermostat placed after each layer. Whatever values arrive, it pulls each feature back to a standard range before passing it on. The layer that follows always sees roughly the same kind of input, from one step to the next.
The original authors called the drift internal covariate shift. It matters most for activation functions that saturate. A sigmoid or tanh is nearly flat when its input is very large or very negative, and gradients vanish there. If the inputs drift far out, the neuron stops learning. Normalizing keeps the inputs in the middle range, where the function is steep and gradients are useful.
The explanation has since been challenged. Santurkar and colleagues (2018) argued that the main benefit is that batch normalization smooths the loss landscape, so that gradient steps are more predictable and larger learning rates are safe. Both views agree on the practical result: the network trains faster and more stably.
- before: mean 3, std 2
- after: mean 0, std 1
The forward pass
For a single feature, the batch gives one value per example. Those values are normalized using the batch's own statistics, then scaled and shifted:
flowchart LR A[Batch of feature values] --> B[Batch mean and variance] B --> C[Subtract mean, divide by std] C --> D[Multiply by gamma, add beta] D --> E[Output to next layer]
Two parameters per feature, gamma and beta, are learned by Gradient Descent like any other weight.
The math
Setup
Take one feature of one layer. In a mini-batch of examples it takes the values , where is the feature's value for example . A small constant (typically ) keeps the division safe when the variance is close to zero.
For a mini-batch of values of one feature, batch normalization computes
The outputs are passed on to the activation function. The learned parameters and are one scale and one shift per feature.
Here is the mean of the batch and is its variance, with the divisor (the population form, which is what the original paper uses). The variable is the standardised value, with mean 0 and variance about 1. The last step is the part that makes the method flexible.
Why the learned scale and shift
Standardising alone would force every feature into the same shape, which could throw away useful information. For instance, a neuron that wants to fire strongly for large inputs may need a spread much bigger than 1.
The learned parameters remove that restriction. If the network sets and , the layer returns exactly the original . So batch normalization never reduces what the layer can represent. It only changes the starting point that gradient descent works from.
Gradients through the batch statistics
During training, and depend on every example in the batch. So the output for example depends on the other examples too, and the gradient of the loss with respect to includes paths through the batch mean and variance. The Chain Rule handles this, and automatic differentiation in deep learning frameworks computes it without extra work from you.
Inference: running averages
At test time there may be one example, or the prediction should not depend on which other examples happen to share its batch. So the layer uses estimates of the mean and variance collected during training. It keeps a running average, updated after every training batch with momentum (often 0.1):
These are exponential moving averages of the batch statistics. At inference the layer computes and then applies and as before. The output for one input no longer depends on the rest of the batch.
Convolutional layers
In a convolutional layer, each channel is one feature. The statistics are taken over the batch and over all spatial positions, so each channel has values in its batch, where and are the height and width of the feature map. There is still one and one per channel, not per pixel.
The original paper placed the step between the linear map and the activation: . The bias of the linear layer becomes redundant, because plays the same role.
Worked examples
One feature takes the values 2, 4, 6, 8 across a batch of examples. Use and , and ignore .
- Mean: .
- Deviations from the mean: .
- Squared deviations: , with sum 20. So .
- Standard deviation: .
- Standardised values: , , , .
- Check: these have mean 0, and variance .
- Scale and shift, : , , , .
Interpretation: the inputs spread from 2 to 8 and the outputs spread from about to . The centre moved to , and the spread is set by . The normalization fixed the starting point, and the network chose the final shape.
A channel in a convolutional layer has a batch of 32 images, each with a feature map. So each channel has values to compute statistics from. Suppose that for this channel the batch mean is and the batch variance is . The learned parameters are and . Take one output value, .
- Standardise: .
- Scale and shift: .
Interpretation: the value 1.8 sits one unit above the batch mean, which is half a standard deviation in standardised units. After the learned transform it becomes 0.95. In the network, the next layer sees a value in a predictable range, whatever the earlier weights happened to produce.
Suppose the running mean starts at 0, the momentum is , and every training batch has mean 5. The update is .
- After batch 1: .
- After batch 2: .
- After batch 3: .
- After 50 batches: .
Interpretation: the running mean lags behind early in training and then converges to the true value. With momentum 0.1, the estimate is effectively an average of the last 10 or so batches. So a model should not be evaluated before its running statistics have settled.
Tradeoffs and failure modes
Batch normalization works well when batches are large and the statistics are stable. It breaks down in several predictable situations:
| Situation | Problem | Better choice |
|---|---|---|
| Very small batches (2 to 4 examples per device) | Batch mean and variance are noisy, so the network trains on noise | Larger batches, synchronised batch norm across devices, or group normalization |
| Recurrent networks and variable-length sequences | Statistics per time step are sparse and depend on sequence length | Layer Normalization |
| Transformers | Batches mix tokens from different sentences, and padding distorts the statistics | Layer Normalization |
| Per-image tasks such as style transfer | Statistics from other images in the batch change each output | Instance normalization |
| Evaluating with the wrong mode, or with running statistics that never settled | Test outputs differ from training behaviour | Switch the layer to inference mode, and recalibrate the statistics on representative data |
Other points to keep in mind:
- The output for one example depends on the others in its batch. This can leak information in some training setups, such as contrastive learning, where the batch is designed to contain specific pairs.
- The running averages are a second source of state. A checkpoint that is saved without them will behave differently when loaded for inference.
- The cost is small. It adds one pass over each batch and two learned vectors per feature, so it is rarely the bottleneck.
History
Before this, the idea of normalizing inputs was already known. LeCun, Bottou, Orr and Müller's "Efficient BackProp" (1998) argued that inputs should be centred and scaled, but that advice applied to the first layer only.
In early 2015, Sergey Ioffe and Christian Szegedy at Google posted "Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift" (arXiv, February 2015, presented at ICML later that year). The problem they were solving was that deep image classifiers were slow and fragile to train, needing small learning rates and careful initialisation. They reported that batch normalization reached the accuracy of a strong baseline in about 14 times fewer training steps, and that an ensemble of their networks set a new best result on ImageNet at the time.
It caught on quickly. The residual networks of 2015 used it, and it became a default in convolutional networks. In 2016 Ba, Kiros and Hinton proposed layer normalization, which computes statistics across features for one example and avoids the batch dependence. That became the standard choice for recurrent networks and transformers. In 2018 Santurkar and colleagues questioned the internal covariate shift explanation, and they showed evidence for the loss-landscape explanation given above.
Check yourself
QuestionFor each feature, which two statistics does batch normalization compute over a mini-batch, and what do the learned parameters gamma and beta do?Show answer
It computes the batch mean and the batch variance . It uses them to standardise each value to mean 0 and variance about 1. Gamma and beta are then a learned scale and shift. They let the layer restore any spread and centre it needs, and if the network chooses and , it recovers the original values exactly.
QuestionA feature takes the values 1, 2, 3 and 6 in a batch. With gamma = 2 and beta = -1, what is the output for the value 6? Ignore epsilon.Show answer
The mean is . The deviations are , with squares and sum 14. So and . The standardised value is . Then .
QuestionWhy does batch normalization use running averages at inference instead of the statistics of the batch being predicted?Show answer
A prediction should not depend on which other examples happen to share its batch, and a batch of one has no meaningful variance. The running averages approximate the mean and variance of the whole training distribution, so the output for an input is deterministic and matches what the network learned during training.
QuestionYou train an object detector with batch size 2 per GPU and batch normalization. Training is noisy and the model does worse at test time than in your validation runs. What is probably going on, and what would you try?Show answer
With only two examples per device, the batch mean and variance are poor estimates, so the layer normalizes with noisy statistics and its running averages do not settle to good values. You could increase the effective batch size by synchronising batch norm across devices, or replace it with group normalization, which does not depend on the batch. You could also check that the model is in inference mode when it is evaluated.
Sources
- Ioffe and Szegedy, Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift (2015): https://arxiv.org/abs/1502.03167
- Santurkar et al., How Does Batch Normalization Help Optimization? (2018): https://arxiv.org/abs/1805.11604
- Batch normalization (Wikipedia): https://en.wikipedia.org/wiki/Batch_normalization
- Goodfellow, Bengio and Courville, Deep Learning, Chapter 8: https://www.deeplearningbook.org/