Search

Search papers and lessons

Batch Normalization

Level 3 · 11 min read

Batch normalization shifts and rescales each feature to mean 0 and variance 1 within every mini-batch, then lets the network learn its own scale and shift, which makes deep nets train faster and more reliably.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Neural network basics
  • Discrete math
  • Sign in to see your progress

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.

−4−20246800.10.20.30.4feature valuedensity
  • before: mean 3, std 2
  • after: mean 0, std 1
Standardising a feature moves its distribution to mean 0 and spread 1. The learned scale and shift then move and stretch it again, to whatever the layer needs.

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 mm examples it takes the values x1,…,xmx_1, \dots, x_m, where xix_i is the feature's value for example ii. A small constant ϵ\epsilon (typically 10−510^{-5}) keeps the division safe when the variance is close to zero.

Definition: Batch normalization

For a mini-batch of values x1,…,xmx_1, \dots, x_m of one feature, batch normalization computes

μB=1m∑i=1mxiσB2=1m∑i=1m(xi−μB)2x^i=xi−μBσB2+ϵyi=γ x^i+β\begin{aligned} \mu_B &= \frac{1}{m} \sum_{i=1}^{m} x_i \\ \sigma_B^2 &= \frac{1}{m} \sum_{i=1}^{m} (x_i - \mu_B)^2 \\ \hat{x}_i &= \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}} \\ y_i &= \gamma \, \hat{x}_i + \beta \end{aligned}

The outputs yiy_i are passed on to the activation function. The learned parameters γ\gamma and β\beta are one scale and one shift per feature.

Here μB\mu_B is the mean of the batch and σB2\sigma_B^2 is its variance, with the divisor mm (the population form, which is what the original paper uses). The variable x^i\hat{x}_i 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 γ=σB2+ϵ\gamma = \sqrt{\sigma_B^2 + \epsilon} and β=μB\beta = \mu_B, the layer returns exactly the original xix_i. 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, μB\mu_B and σB2\sigma_B^2 depend on every example in the batch. So the output for example ii depends on the other examples too, and the gradient of the loss with respect to xix_i 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 α\alpha (often 0.1):

μrun←(1−α) μrun+α μBσrun2←(1−α) σrun2+α σB2\begin{aligned} \mu_{\text{run}} &\leftarrow (1 - \alpha)\, \mu_{\text{run}} + \alpha\, \mu_B \\ \sigma^2_{\text{run}} &\leftarrow (1 - \alpha)\, \sigma^2_{\text{run}} + \alpha\, \sigma^2_B \end{aligned}

These are exponential moving averages of the batch statistics. At inference the layer computes x^=(x−μrun)/σrun2+ϵ\hat{x} = (x - \mu_{\text{run}}) / \sqrt{\sigma^2_{\text{run}} + \epsilon} and then applies γ\gamma and β\beta 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 m⋅H⋅Wm \cdot H \cdot W values in its batch, where HH and WW are the height and width of the feature map. There is still one γ\gamma and one β\beta per channel, not per pixel.

The original paper placed the step between the linear map and the activation: y=ϕ(BN(Wu))y = \phi(\mathrm{BN}(Wu)). The bias of the linear layer becomes redundant, because β\beta plays the same role.

Worked examples

Example
A tiny batch by hand

One feature takes the values 2, 4, 6, 8 across a batch of m=4m = 4 examples. Use γ=2\gamma = 2 and β=1\beta = 1, and ignore ϵ\epsilon.

  1. Mean: μB=(2+4+6+8)/4=5\mu_B = (2 + 4 + 6 + 8) / 4 = 5.
  2. Deviations from the mean: −3,−1,1,3-3, -1, 1, 3.
  3. Squared deviations: 9,1,1,99, 1, 1, 9, with sum 20. So σB2=20/4=5\sigma_B^2 = 20 / 4 = 5.
  4. Standard deviation: σB=5≈2.2361\sigma_B = \sqrt{5} \approx 2.2361.
  5. Standardised values: −1.3416-1.3416, −0.4472-0.4472, 0.44720.4472, 1.34161.3416.
  6. Check: these have mean 0, and variance (1.8+0.2+0.2+1.8)/4=1(1.8 + 0.2 + 0.2 + 1.8) / 4 = 1.
  7. Scale and shift, y=2x^+1y = 2 \hat{x} + 1: −1.6833-1.6833, 0.10560.1056, 1.89441.8944, 3.68333.6833.

Interpretation: the inputs spread from 2 to 8 and the outputs spread from about −1.7-1.7 to 3.73.7. The centre moved to β=1\beta = 1, and the spread is set by γ=2\gamma = 2. The normalization fixed the starting point, and the network chose the final shape.

Example
A convolutional channel in a real network

A channel in a convolutional layer has a batch of 32 images, each with a 28×2828 \times 28 feature map. So each channel has 32×28×28=25,08832 \times 28 \times 28 = 25{,}088 values to compute statistics from. Suppose that for this channel the batch mean is μB=0.8\mu_B = 0.8 and the batch variance is σB2=4.0\sigma_B^2 = 4.0. The learned parameters are γ=1.5\gamma = 1.5 and β=0.2\beta = 0.2. Take one output value, x=1.8x = 1.8.

  1. Standardise: x^=(1.8−0.8)/4.0+10−5≈1.0/2.000003≈0.5000\hat{x} = (1.8 - 0.8) / \sqrt{4.0 + 10^{-5}} \approx 1.0 / 2.000003 \approx 0.5000.
  2. Scale and shift: y=1.5×0.5000+0.2=0.95y = 1.5 \times 0.5000 + 0.2 = 0.95.

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.

Example
Running averages at inference

Suppose the running mean starts at 0, the momentum is α=0.1\alpha = 0.1, and every training batch has mean 5. The update is μrun←0.9 μrun+0.1×5\mu_{\text{run}} \leftarrow 0.9 \, \mu_{\text{run}} + 0.1 \times 5.

  1. After batch 1: 0.9×0+0.5=0.50.9 \times 0 + 0.5 = 0.5.
  2. After batch 2: 0.9×0.5+0.5=0.950.9 \times 0.5 + 0.5 = 0.95.
  3. After batch 3: 0.9×0.95+0.5=1.3550.9 \times 0.95 + 0.5 = 1.355.
  4. After 50 batches: μrun=5(1−0.950)≈5×0.9948≈4.974\mu_{\text{run}} = 5 (1 - 0.9^{50}) \approx 5 \times 0.9948 \approx 4.974.

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:

SituationProblemBetter choice
Very small batches (2 to 4 examples per device)Batch mean and variance are noisy, so the network trains on noiseLarger batches, synchronised batch norm across devices, or group normalization
Recurrent networks and variable-length sequencesStatistics per time step are sparse and depend on sequence lengthLayer Normalization
TransformersBatches mix tokens from different sentences, and padding distorts the statisticsLayer Normalization
Per-image tasks such as style transferStatistics from other images in the batch change each outputInstance normalization
Evaluating with the wrong mode, or with running statistics that never settledTest outputs differ from training behaviourSwitch 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 μB\mu_B and the batch variance σB2\sigma_B^2. 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 γ=σB2+ϵ\gamma = \sqrt{\sigma_B^2 + \epsilon} and β=μB\beta = \mu_B, 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 μB=3\mu_B = 3. The deviations are −2,−1,0,3-2, -1, 0, 3, with squares 4,1,0,94, 1, 0, 9 and sum 14. So σB2=14/4=3.5\sigma_B^2 = 14 / 4 = 3.5 and σB≈1.8708\sigma_B \approx 1.8708. The standardised value is (6−3)/1.8708≈1.6036(6 - 3) / 1.8708 \approx 1.6036. Then y=2×1.6036−1≈2.2071y = 2 \times 1.6036 - 1 \approx 2.2071.

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