BatchNorm, step by step: normalize each feature over the batch, then scale and shift

A minibatch of N examples, each with D = 2 features. BatchNorm subtracts each feature’s batch mean, divides by its batch standard deviation, then applies a learned scale γ and shift β. Drag any example in the input and watch every other example move in the output.

One minibatch through BatchNorm Three scatter plots of the same minibatch. Left, the input x with each feature's batch mean and one standard deviation marked. Middle, the normalized x-hat, centred at zero with unit spread on each feature. Right, the output gamma times x-hat plus beta, on the same axes as the input, with the input shown faintly behind it.
batch size N
γ1 β1 γ2 β2

At test time: running averages replace the batch

At test time there may be no batch at all, and an example’s answer must not depend on whatever else happens to be with it. So during training BatchNorm also keeps an exponential moving average of each batch’s mean and variance (the EMA from the optimizers unit), and at test time it normalizes with those instead.

Running averages over training steps Three strips sharing a horizontal axis of training steps. Top, each batch's mean of feature 1 as a dot and the running mean as a line converging to the population mean. Middle, the same for the standard deviation. Bottom, one fixed example's normalized value as training would compute it inside each batch, scattered around the single value test time gives it.
batch size
momentum
batches

Folding into the layer before: at test time BatchNorm is just an affine map

With the running μ and σ² frozen, BN(z) = γ(z − μ)/√(σ²+ε) + β is a fixed scale and shift of each feature. If z = Wx + b comes from a linear layer, fold the two together: W′ = (γ/√(σ²+ε))·W row by row, and b′ = γ(b − μ)/√(σ²+ε) + β. One layer, same answers, no BatchNorm left to run.