7 routes42 sections324 units

Unit 3.3 · about 4 h

187 / 324

Batch, layer, group and RMS normalization

Normalization decides which axes share statistics and whether evaluation depends on the batch. The wrong choice breaks single-example prediction or leaks batch mates into each output.

  • derive
  • implement

You can now

  • Derive the normalized output, parameter count and normalized axes for batch, layer, group and RMS normalization.
  • Implement layer normalization from tensor ops and match the reference on random input.
  • Predict when batch statistics help, hurt or break, and choose the matching normalization.

Covers

  • Batch, layer, group and RMS normalization axes

Study, in this order

5 steps

  1. Batch, layer and prediction behavior
    D2L - optimization and regularization slice

    Batch Normalization Layers (fully connected and convolutional), Layer Normalization, and Batch Normalization During Prediction

  2. Batch normalization (equations and behavior)
    Understanding Deep Learning - fitting, gradients and regularization

    Chapter 11, Section 11.4 (pp. 192 to 194): batch equations, train versus test statistics, and costs with benefits

  3. Group normalization axes
    PyTorch GroupNorm reference

    GroupNorm page: channel grouping, normalized axes and the affine parameters

  4. RMS normalization formula
    PyTorch RMSNorm reference

    RMSNorm page: RMS formula, normalized_shape axes, and absent centering

  5. HOML: batch norm and layer norm in PyTorch
    Hands-On Machine Learning with Scikit-Learn and PyTorch

    Chapter 11, Batch Normalization and Layer Normalization (printed pp. 375-382)

    Depth · Read it for the PyTorch conventions: the BN momentum is the weight of the new batch statistic (default 0.1), the opposite of the usual running-average meaning; BatchNorm1d expects [batch, features, length]; nn.LayerNorm normalizes the trailing normalized_shape dimensions, with an explicit unbiased=False equivalent on p. 381.

Focus on

  • Batch norm normalizes each channel over batch and spatial axes with learned per-channel scale and shift. One example's training output depends on the other batch members.
  • Layer norm normalizes one example over its features with no running statistics, so train and test match. Group norm sits between: \(G\) channel groups per example, with \(G = C\) giving per-channel behavior.
  • RMS norm drops mean centering and bias and scales by the root mean square only. Fewer statistics, same axis argument.
  • PyTorch tracks batch statistics as exponential running averages during training. D2L's full-dataset recomputation after training describes the goal, not the implementation.

Reproduce closed-book

Closed-book, implement layer normalization for a (batch, features) input from sums only. Then compute batch norm by hand for values [0, 2, 4] with unit scale, zero shift and zero \(\epsilon\): outputs -1.225, 0 and 1.225.

Traps

  • Normalizing with batch statistics at test time. Eval must use the tracked running statistics; with a batch of one, a fully connected batch-norm layer would map every feature to its shift parameter.
  • Keeping a bias right before batch normalization. Batch norm subtracts the per-channel batch mean, which removes any per-channel constant, so that bias is redundant (a D2L exercise asks this). Before layer norm a per-feature bias is not fully removed, because layer norm subtracts one mean across features.
  • Treating RMS norm as layer norm without bias. It also removes mean centering, so a large constant offset passes through scaled but unshifted.

Practice

TrenTorch: Layer Normalization, forward

Implement layer_norm_forward(x, gamma, beta, eps) over the last axis; one test compares against torch.nn.LayerNorm

Use the biased variance (divide by \(n\)) with \(\epsilon\) inside the square root; a test fails on the \(n - 1\) version.

D2L Batch Normalization from Scratch and Concise implementation

Implementation from Scratch and Concise Implementation: build batch norm, match the concise layer, and answer the bias-removal exercise

Self-test

  1. Batch values [0, 2, 4], \(\gamma\) 1, \(\beta\) 0, \(\epsilon\) 0. What are the normalized outputs?

  2. Conv input \((N, C, H, W)\). Which axes does batch norm use per channel, and why does batch size 1 still work?

  3. Group norm with \(G = 1\) versus \(G = C\) on one example. What does each compute?

  4. RMS norm input [3, 4], \(\gamma\) 1. What is the output?

Completion criterion

Stop when

Your from-scratch layer norm matches the framework output on random input, and you state each method's axes and eval behavior without notes.

Mark passed, then open 3.4 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Understanding Deep Learning - fitting, gradients and regularization
    Simon J. D. Princeprimarytheory
    Use Chapters 6-9 and targeted parts of Chapter 20 for optimization, initialization, measurement, regularization and why training works. Re-derive the update rules you keep in the trainer.
  • D2L - optimization and regularization slice
    D2L authorsrequiredimplementation
    Use MLP implementation, dropout, deep generalization, numerical stability, optimization introduction, SGD, minibatch SGD, momentum, AdaGrad, RMSProp, Adam and learning-rate scheduler. Implement momentum, AdamW, initialization, dropout, one normalization, clipping and one schedule.
  • PyTorch GroupNorm reference
    PyTorchreferencereference
    Read how input channels are split into num_groups groups and which axes each group normalizes; compare num_groups 1 against num_channels.
  • PyTorch RMSNorm reference
    PyTorchreferencereference
    Read the RMS formula and which axes normalized_shape covers; note the missing mean centering and bias.
  • Hands-On Machine Learning with Scikit-Learn and PyTorch
    Aurelien Geron (O'Reilly, 2025)supporttheory
    Read the Chapter 11 sections named in the units for the PyTorch side of initialization, normalization, clipping and schedules.

Also in this section

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels