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.
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.
batch sizemomentumbatches
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.