3.2.9 · D4Training Deep Networks

Exercises — Layer normalization

2,319 words11 min readBack to topic

Throughout, one example is a single vector with features. The core recipe (from the parent) is:

Here is the mean (the balance-point of the row), is the variance (average squared distance from that balance-point), is the standardized value, and are the learnable scale and shift. The tiny constant guards against dividing by zero.

Figure — Layer normalization

The figure above is our mental model: a matrix whose rows are examples and columns are features. Look at the red row-arrow — that is the direction LayerNorm averages along. The blue column-arrow is what BatchNorm would use instead. Keep this picture handy for every question.


L1 · Recognition

Recall Solution 1.1

Across a row. LayerNorm takes one example (one row) and averages over all its features. It never looks at other examples. (BatchNorm is the one that goes down a column — same feature, many examples.) See the red arrow in the figure above.

Recall Solution 1.2
  • = the standardized activation (mean 0, variance 1 over the row). Not learnable — computed from the data.
  • = the learnable scale. Learnable.
  • = the learnable shift. Learnable. So the network stretches () and slides () the normalized values to whatever range suits the next layer.
Recall Solution 1.3

guards the division, which blows up when (a near-constant row). Placing it under the root, , guarantees the denominator is at least even when . If you added it after the root, , then when is exactly the denominator is (fine) — but the standard convention puts it inside so the whole quantity stays a clean "variance plus floor." Either way the point is: never divide by zero.


L2 · Application

Recall Solution 2.1
  • Mean: .
  • Deviations: .
  • Variance: squares , mean , so ,
  • Standardize: . Check: sum ✓ and mean of squares ✓.
Recall Solution 2.2

: Mean of : since each averages to 0, . The affine step deliberately gives the row a new mean () and a new standard deviation ().

Recall Solution 2.3
  • Mean: . Deviations: all . Variance: .
  • Standardize: every numerator is , so . Without this would be NaN. With , a constant row cleanly maps to all zeros — no crash. This is exactly the degenerate case the parent's third mistake warned about.

L3 · Analysis

Recall Solution 3.1

New mean: . New deviation: . New variance: , so (since ). Then The and cancel exactly. Interpretation: if an earlier layer suddenly scales or offsets all its outputs, LayerNorm erases that change — the next layer sees identical inputs. This is the stability that fights Vanishing and exploding gradients.

Recall Solution 3.2

We need . Since (rearranging the standardize step), matching gives Concretely with : ✓, recovering . So the affine step is expressive enough to make LayerNorm a no-op — proving are not redundant.

Recall Solution 3.3

BatchNorm's statistics for a feature are averaged down the column — over all examples in the batch. With batch size 1 the column has a single number, so its "variance" is and the mean is just that number: standardizing gives , destroying all information (or NaN without ). LayerNorm averages across the row of that single example's features, which is always well-defined regardless of batch size. That is precisely why Transformers and RNNs adopt LayerNorm. See Internal covariate shift for the original motivation both methods share.


L4 · Synthesis

Recall Solution 4.1

RMS: , so . Compare first entries: LayerNorm gave ; RMSNorm gives . They differ because RMSNorm skips centering (), so it keeps the sign/offset of the raw value; LayerNorm re-centers first, flipping (below the mean ) to a negative standardized value. RMSNorm is cheaper (no mean) and often works comparably in Transformers.

Recall Solution 4.2

This is — a pure scaling of Ex. 2.1 by . By the invariance proof (Ex. 3.1), LayerNorm erases the factor of : identical to Ex. 2.1. Lesson: even if the residual branch produces huge activations, the LayerNorm hands the next block bounded numbers — this is why LayerNorm placement tames Vanishing and exploding gradients in deep stacks.

Recall Solution 4.3

Since has mean 0 and std 1, applying gives mean and std . To hit mean , std : Check on Ex. 2.1's : , and the four outputs have mean and std . The affine step is just a linear map from the standard template to any you like.


L5 · Mastery

Recall Solution 5.1

Write with , . Both numerator and denominator are homogeneous of degree 1 in : scaling makes and . So is homogeneous of degree 0 — invariant. A degree-0 function's gradient is homogeneous of degree : ... but the derivative is taken with respect to the scaled coordinate, and by the chain rule that extra cancels — the derivative of w.r.t. the input entry is scale-free. Explicitly, differentiating gives For : , , so For (): , , giving wait — recompute: , so . The raw derivative shrinks by — but the gradient the optimizer sees, , is exactly again once you account for the coordinate scaling. Takeaway: LayerNorm makes the effective learning signal insensitive to the overall magnitude of a layer's activations — a key reason it stabilizes optimization.

Recall Solution 5.2

We know (from the parent's variance identity, since mean-square ). For any single index , ; the other terms are , but we can do better. The most extreme case puts all "mass" in one coordinate against the mean-zero constraint . Maximizing subject to and gives the other entries equal, and solving yields , so . For : bound is . Our largest standardized value is ✓. So no standardized activation can explode — a built-in ceiling that helps prevent Vanishing and exploding gradients.

Recall Solution 5.3
  1. Batch independence (Ex. 3.3): generation uses batch size 1; BatchNorm's per-column statistics collapse, LayerNorm's per-row statistics are always well-defined.
  2. Train = inference (parent, mistake 4): LayerNorm needs no running averages, so the model behaves identically while training and while decoding — no train/test mismatch.
  3. Scale stability around residuals (Ex. 4.2): deep Transformer stacks with residual branches can produce large activations; LayerNorm's scale-invariance (Ex. 3.1) hands each block bounded inputs, controlling Vanishing and exploding gradients. (Bonus: swapping to RMSNorm keeps points 1–3 while saving the mean computation.)

Recall One-screen recap
  • Axis: LayerNorm normalizes across the row (features of one example); BatchNorm down the column.
  • Recipe: subtract , divide by (divisor , not ), then .
  • Invariance: immune to shifting/scaling the whole vector (Ex. 3.1) — not to per-feature scaling (L3 trap).
  • can fully undo normalization ⇒ not redundant (Ex. 3.2).
  • Bounds: (Ex. 5.2). Constant row → all zeros thanks to (Ex. 2.3).
  • Why Transformers use it: batch-independent, train = inference, tames residual blow-ups.