3.2.8 · D4Training Deep Networks

Exercises — Batch normalization

2,428 words11 min readBack to topic

Before we start, one shared reminder of the machine we are testing — the four steps, drawn as a pipeline.

Figure — Batch normalization

Read the picture left to right: raw activations enter, we measure their mean (the balance point) and variance (the spread), subtract the mean and divide by the spread to get (centered, unit-width), then the two learnable dials (gain) and (shift) place the result wherever training wants it: .

So the core formula, with both symbols now earned, reads


Level 1 — Recognition

Recall Solution 1.1

The mean comes first. Why? Variance is defined as the average of squared deviations from the mean, (recall = batch size). You literally cannot compute a deviation until you know what you are deviating from. So is a prerequisite for . In the figure above, the "measure balance point" box must fire before the "measure spread" box.

Recall Solution 1.2

Training → (b) mini-batch statistics. Each mini-batch gets its own . This makes the output of BN depend on the other examples in the batch — a form of noise that acts as a mild regularizer. Inference → running (population) statistics , the exponential moving averages accumulated during training. Why the switch? At test time we may see one example at a time — no batch exists to average over — and we need the output to be deterministic.

Recall Solution 1.3

False. BN's first act is subtracting the batch mean. Adding a constant to every activation shifts every and shifts by exactly the same , so the deviation is unchanged. The bias cancels perfectly. Its job is instead done by the learnable . Practical rule: set bias=False in the layer feeding into BN.


Level 2 — Application

Recall Solution 2.1

Step 1 — mean. . Step 2 — variance (biased, with ). deviations ; squares ; mean . So , . Step 3 — normalize. Since , : Sanity: mean of , variance . ✓ (Compare to Example 1 in the parent — same shape, just a wider input, so the same standardized pattern comes out.)

Recall Solution 2.2

Apply : Why: stretches the unit-variance signal to standard deviation , and slides the whole thing down by . So the output has mean and standard deviation .

Recall Solution 2.3

No batch — use the frozen running stats. The map is now a fixed affine function; the same always yields regardless of any neighbours (there are none).


Level 3 — Analysis

Recall Solution 3.1

Deviations all . So for all . The turned a lethal into a safe — exactly the safety role defined at the top of the page. Information destroyed: the common value — anything constant across the batch is centered to and erased. Small-batch link: with few examples, batches are more likely to be near-constant or have wildly swinging from step to step, so the normalization is noisy/unreliable — motivating Layer Normalization or Group Normalization in that regime.

Recall Solution 3.2

To undo normalization we invert Step 4: we need . Since , choosing gives . ✓ Check : ; . ✓ The identity is reachable, so BN adds no restriction — it only changes the optimization landscape.

Recall Solution 3.3

Why the forms: , so and . Both and are shared across all examples, so by the chain rule their total gradient is the sum of each example's contribution: and . (See Backpropagation.) Numbers with : Term by term: . So .


Level 4 — Synthesis

Recall Solution 4.1

With : pre-BN values . Mean ; deviations ; variance ; . Normalized: . With : pre-BN values . Mean ; deviations ; same variance ; normalized . Identical. Why: adding shifted every value and the mean by , leaving deviations untouched, and variance is a function of deviations only. Conclusion: the bias before BN is dead weight — confirms Exercise 1.3 quantitatively.

Recall Solution 4.2

Each step: .

  • After batch 1: .
  • After batch 2: .
  • After batch 3: . Why so far below the true ~10? With small the running average has long memory and warms up slowly; early in training the estimate lags. This is why running stats are unreliable if used too early, and why some frameworks debias them.
Recall Solution 4.3

Expand : . . So . Check with : — matches Exercise 2.3. ✓ Because it is pure affine, this can be absorbed into the preceding linear layer's weights and bias, giving BN zero inference cost.


Level 5 — Mastery

Recall Solution 5.1

Choose Layer Normalization (Transformers' standard), or Group Normalization for vision with tiny batches. Why not BN: with batch size , are estimated from just two numbers — catastrophically noisy (Exercise 3.1's lesson taken to the limit); and single-example inference forces reliance on possibly-unwarmed running stats (Exercise 4.2). The axis rule: BN averages across the batch (same feature, many examples); Layer Norm averages across the features of a single example — so it is completely batch-independent, identical at train and test. That independence is exactly what small-batch / streaming settings need. This connects to Vanishing and Exploding Gradients and Optimization and Conditioning of the Loss Surface: all three normalizers aim at a better-conditioned surface, but by averaging over different axes.

Recall Solution 5.2

For a constant batch every , so and each deviation exactly (not just in the limit). Then The denominator is strictly positive (since ), so no division blows up and the numerator being forces regardless of . Interpretation: the value of a constant feature is invisible to BN — it collapses to the learned shift . The signal that survives BN is only the relative spread within the batch, which is why BN cannot help when a batch carries no within-batch variation.

Recall Solution 5.3

Prediction: is irrelevant (Ex 1.3 / 4.1) — BN removes it. So compute as if pre-BN values were 's the deviations only... but let's include honestly and watch it cancel. Pre-BN: . Mean . Deviations . Variance ; . . . Same (and hence ) as if — the huge bias shifted the mean by and vanished in the deviations. Prediction confirmed. ✓


Recall Final self-check (cover the answers)

Which comes first, mean or variance? ::: Mean — variance needs deviations from it. Training uses which stats? ::: Per-mini-batch . Inference uses which stats? ::: Running (population) . Why drop the bias before BN? ::: BN subtracts the mean, cancelling any constant bias; replaces it. Which estimator for variance? ::: Biased, divide by (batch size), not . Tiny-batch / streaming fix? ::: Use Layer Normalization or Group Normalization.