Batch Normalisation: Train vs Inference

medium
Unit 17 · Normalisation0/3 to milestone

Batch normalisation normalises each feature over the batch, then applies a learned scale γ\gamma and shift β\beta:

y=γ⋅x−μσ2+ϵ+βy = \gamma \cdot \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} + \beta

The subtle part is that it behaves differently in training and inference.

Implement BatchNorm1d(num_features, momentum=0.1, eps=1e-5) with gamma (ones), beta (zeros), running_mean (zeros) and running_var (ones), and a method forward(x, training=True) for x of shape (batch, features):

  • Training: normalise with the batch's own mean and (population, ddof=0) variance. Then update the running statistics: running = (1 - momentum) * running + momentum * batch_stat. Like PyTorch, use the unbiased variance (ddof=1) for running_var when the batch has more than one row.
  • Inference: normalise with running_mean and running_var, and don't update them.
#normalisation#training#numpy3 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Batch Normalisation: Train vs Inference · TechGradify