Skip to content

3.4 Initialization and normalization

ready≈ 11.5 hoursNeeds: 3.2Unlocks: 3.5, 3.7, 4.5
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Activation histogram lab)
  4. 4 Do
  5. 5 Check

Variance through depth. A unit sums nn inputs times weights, so with independent weights of variance σ2\sigma^2 its pre-activation has variance about nσ2n\sigma^2 times that of the inputs. A ReLU then zeroes half of it. Unless nσ2/2=1n\sigma^2/2 = 1, the scale shrinks or grows by the same factor at every layer: geometrically, so after 50 layers it is either zero or overflow, and the gradients flowing back do the same (UDL §7.5).

He initialization sets σ2=2/nin\sigma^2 = 2/n_\text{in} for ReLU layers (He et al. 2015). Xavier initialization uses about 1/nin1/n_\text{in} for tanh, which is roughly linear near zero (Glorot and Bengio 2010). Set the depth to 40 in the lab and switch between them.

BatchNorm stops relying on the initialization. For each feature it subtracts the batch mean and divides by the batch standard deviation, then applies a learned scale γ\gamma and shift β\beta so the layer can still represent any mean and variance (Ioffe and Szegedy 2015; UDL §11.4). The catch is that an example’s output depends on the rest of its batch. At evaluation time there may be no batch, so BatchNorm keeps running averages of the statistics during training and uses those instead. Forgetting to switch modes is a classic bug.

LayerNorm normalises each example over its own features instead (Ba et al. 2016). It needs no batch statistics, so training and evaluation behave the same and it works for sequences of any length. That is why transformers use it, or its cheaper cousin RMSNorm, which skips the mean (Zhang and Sennrich 2019).

The backward pass is the interesting part: the mean and variance depend on every example in the batch, so each input’s gradient picks up two extra terms (Ioffe and Szegedy 2015, §3).

BatchNorm backward passstep 1 of 6
  1. μ=1N∑ixi,σ2=1N∑i(xi−μ)2,s=σ2+ϵ,x^i=xi−μs,yi=γx^i+β\mu = \tfrac1N\textstyle\sum_i x_i,\quad \sigma^2 = \tfrac1N\sum_i (x_i-\mu)^2,\quad s = \sqrt{\sigma^2+\epsilon},\quad \hat x_i = \frac{x_i-\mu}{s},\quad y_i = \gamma\hat x_i + \beta

Activation histogram lab

Vanishing and exploding activations; why normalization helps

Static view on small screens. Open this page on a laptop to interact.

1e101e51e01e-51e-101e-151e-20layer 1 → 30: standard deviation of the activations (log scale)
layer 10 … 1e+0layer 70 … 2e-3layer 130 … 3e-6layer 180 … 1e-8layer 240 … 2e-11layer 300 … 2e-14
std at layer 1 2.85e-1 std at layer 30 5.95e-15 units at exactly 0 in the last layer 55%

A batch of 64 Gaussian inputs passes through layers of 64 units with random weights of standard deviation gain/√64. With ReLU, gain √2 (He) keeps the scale steady; a smaller gain shrinks it geometrically and a larger one blows it up, which is what happens to gradients too. With tanh, a large gain saturates units at ±1, where the gradient is nearly zero. Normalization resets every layer to mean 0 and variance 1 before the activation, whatever the weights. Histograms of positive ReLU outputs are scaled to each layer's own range.

A random batch is passed through up to 40 layers of 64 units. The top chart plots the standard deviation of the activations at each layer on a log scale. Below, histograms show the activations at six layers from first to last. Controls set the initialization scale (from too small through Xavier and He to too large), the activation, the depth, and whether BatchNorm or LayerNorm is applied before each activation. Keyboard: Tab through the controls; arrow keys adjust the depth slider.

Implement He initialization, then BatchNorm and LayerNorm with their backward passes. The tests check the normalised statistics, run both backward passes through a gradient checker, and make sure evaluation mode uses the running statistics.

Initialization and normalization stubs/m3_4_norms.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m3_4_norms.py and run mlc check 3.4. The tests are identical; import the results on the progress page.

Hints · batchnorm_backward 0/3 revealed
Hints · running statistics 0/3 revealed
Hints · layernorm_backward 0/3 revealed

Locally: mlc check 3.4.

1. Layers of 512 ReLU units have weights drawn with standard deviation 0.01. What happens to the activations after 50 layers?
2. What weight variance does He initialization use for a ReLU layer with n inputs?
3. Your model scores well in training but its accuracy collapses when you evaluate one image at a time. The likely cause?
4. Why do transformers use LayerNorm rather than BatchNorm?
5. Why do BatchNorm and LayerNorm learn γ and β?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.