Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Deep learning layers

Batch normalisation in training mode

Medium · Target 15 minutes · Python and NumPy

Given a batch X (N × D, N ≥ 2), parameters gamma, beta, and running estimates running_mean, running_var (each length D), return [out, new_mean, new_var] where

  • out = γ ⊙ (X − μ) / √(σ² + eps) + β, with μ and σ² the per-feature mean and biased variance over the batch;
  • new_mean = (1 − momentum)·running_mean + momentum·μ;
  • new_var = (1 − momentum)·running_var + momentum·σ²unbiased, where σ²unbiased = σ²·N/(N − 1).

These are PyTorch's conventions for BatchNorm1d in training mode.

Examples

Input X = [[1, 10], [3, 30]], gamma = [1, 1], beta = [0, 0], running_mean = [0, 0], running_var = [1, 1], momentum = 0.1, eps = 0

Output [[[-1, -1], [1, 1]], [0.2, 2], [1.1, 20.9]]

Two examples: each feature becomes (−1, 1). The running variance moves towards 2 and 200, not 1 and 100.

Input X = [[1], [2], [3], [4]], gamma = [2], beta = [1], running_mean = [0], running_var = [1], momentum = 1, eps = 1.000e-5

Output [[[-1.6833], [0.1056], [1.8944], [3.6833]], [2.5], [1.6667]]

Momentum 1 replaces the running statistics outright.

Input X = [[0.5, -1], [0.5, 3]], gamma = [1, 1], beta = [0, 0], running_mean = [10, 10], running_var = [5, 5], momentum = 0, eps = 1.000e-5

Output [[[0, -1], [0, 1]], [10, 10], [5, 5]]

Momentum 0 leaves them untouched. The constant first feature normalises to 0.

Constraints

  • 2 ≤ N ≤ 512, 1 ≤ D ≤ 512
  • 0 ≤ momentum ≤ 1
  • Checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Statistics are over axis 0, the batch. Each feature has its own mean and variance.

Hint 2

Normalise with X.var(axis=0), which divides by N.

Hint 3

For the running variance, rescale the biased variance by N/(N − 1) before mixing it in.

Where candidates go wrong

  • Normalising with the unbiased variance, or tracking the biased one. Both are plausible and both disagree with PyTorch.
  • Reading momentum the Keras way: Keras keeps momentum·old + (1 − momentum)·new, the opposite convention.
  • Mutating running_mean in place and returning the same list, so the caller's copy changes too.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

Batch norm has a training mode and an inference mode, and the bugs live in the difference. The question checks the axis, the momentum convention and the two different variances, which between them account for most real batch-norm bugs.

The theory behind it is in the lesson Attention, normalisation and convolution from scratch.


Keep going