Batch norm and layer norm
Quick answer
Normalization layers rescale activations, then optionally learn affine scale and offset. Batch normalization uses batch statistics during training and typically running statistics in evaluation. Layer normalization uses features within each example in both modes. Examples are static Python snippets tested locally on PyTorch CPU. ByteMind execution is unverified.
BatchNorm statistics and mode
import torchx=torch.tensor([[1.,10.],[3.,14.]])b=torch.nn.BatchNorm1d(2,affine=False,momentum=1.,eps=1e-5)print([[round(v,4) for v in row] for row in b(x).tolist()]); print(b.running_var.tolist())b.eval(); print([round(v,4) for v in b(x)[0].tolist()])Expected output (locally tested): [[-1.0, -1.0], [1.0, 1.0]] [2.0, 8.0] [-0.7071, -0.7071]. Positive epsilon stabilizes the denominator; rounded values illustrate biased versus unbiased variance.
How it works
For a vector of values, normalize by (x-mean)/sqrt(var+eps), then multiply by learned gamma and add beta. For inputs (N,C), BatchNorm1d computes statistics per channel across N; with (N,C,L), across N and L. Training normalization uses biased variance (divide by count), but the running variance update uses an unbiased estimate. Running mean update is (1-momentum)old+momentumbatch_mean; this momentum is unrelated to optimizer momentum. In default eval mode running statistics are used, not test batch statistics. With track_running_stats=False batch statistics are used even in evaluation. LayerNorm(C) on (N,C) normalizes each row over C, with the same rule in train/eval and biased variance. It does not need running statistics. Epsilon stabilizes constant groups; normalized variance is not exactly one with nonzero epsilon. Normalization helps optimization in many architectures but is not universally beneficial.
LayerNorm within examples
import torchx=torch.tensor([[1.,3.],[10.,14.]])l=torch.nn.LayerNorm(2,elementwise_affine=False,eps=1e-5)a=l(x); l.eval(); print([[round(v,4) for v in row] for row in a.tolist()],torch.equal(a,l(x)))Expected output (locally tested): [[-1.0, 1.0], [-1.0, 1.0]] True. Each row is normalized independently with positive epsilon; outputs are rounded for readability.
Common mistakes
Do not normalize across the wrong axis or call eval data with a training BatchNorm layer: it can change running statistics. A default training BatchNorm1d on (1,C) has insufficient values per channel. LayerNorm is not a substitute for preprocessing fitted on training data.
Practice and next steps
On a two-row matrix, compute the per-column and per-row means. Compare results with affine=False to isolate normalization.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Batch norm and layer norm — knowledge check
Apply batch norm and layer norm to small examples and debugging scenarios.
For (N,C), which axis supplies BatchNorm1d training statistics?
Which variance does PyTorch BatchNorm use for running variance updates?
What does default BatchNorm use during evaluation?
What does LayerNorm(C) normalize on (N,C)?
What does epsilon protect against?