Normalise, then undo it on purpose
Batch normalisation standardises each feature across the examples in the batch, then hands the network the means to reverse that if it wants to.
- : how many examples are in the batch.
- : the mean and variance of one feature over those examples.
- : a small constant, about , so a constant feature does not divide by zero.
- : learned scale and shift, one pair per feature.
Why add parameters that undo the normalisation
Forcing every activation to mean zero and variance one is a real constraint, and sometimes a harmful one.
A sigmoid fed only values near zero is operating in its linear region, which throws away the nonlinearity the layer exists to provide.
With and the layer can learn any mean and any variance it likes, including the original ones.
So the normalisation is a starting point the network can move away from, not a cage.
The axis, which is the whole difference from layer norm
The statistics are computed across the batch, separately for each feature.
For a batch of examples with features, that is means and variances, each averaged over numbers.
That is why the batch size matters: with the estimates of mean and variance are almost noise, and the layer injects that noise into every forward pass.
Training and inference are different functions
At training time the statistics come from the batch. At inference there may be only one example, and its own mean is meaningless.
So training also keeps a running average of the statistics, and inference uses those fixed numbers instead.
This is the single most common source of "it works in training and not in production": the model was left in training mode, or the running statistics never converged.
One forward pass, in order
- Compute the mean of each feature over the batch.
- Compute the variance of each feature over the batch.
- Subtract the mean and divide by the root of the variance plus epsilon.
- Scale by the learned gamma and add the learned beta.
- Update the running statistics for use at inference time.