3.2.9 · D3Training Deep Networks

Worked examples — Layer normalization

2,845 words13 min readBack to topic

This page is a drill of cases. The Layer normalization parent note built the machine; here we run every kind of input through it so you never meet a surprise. We compute LayerNorm by hand for tiny vectors, then push it into the strange corners: a flat (constant) vector, a two-value vector, a single-feature row, negatives, huge scales, the affine layer, and a word problem.

Everything uses only what the parent defined. As a one-line reminder, for one example's vector :


The scenario matrix

Every input a LayerNorm layer can receive falls into one of these cells. The examples below are labelled with the cell(s) they cover, so by the end every row of this table has a worked case.

Cell What makes it special Where the danger is Covered by
A. Ordinary spread mixed positive values, none — the "textbook" run Ex 1
B. Pure scaling same shape as A but is it really scale-invariant? Ex 2
C. Pure shift same shape but to every entry does adding a constant change ? Ex 3
D. Negatives + asymmetry some , lopsided sign bookkeeping in deviations Ex 4
E. Two features, smallest non-trivial row collapses to Ex 5
F. Degenerate / constant all equal, division by ~zero — the case Ex 6
F′. Single feature, one number, always same rescue, output Ex 6b
G. Affine restore apply ; also "undo" knobs re-shape the output Ex 7
H. Word problem real activations drift up 100 why this stabilises the next layer Ex 8
I. Exam twist solve backwards for invert the affine step Ex 9

The examples

Examples 1–3 are three different raw rows that collapse onto one normalized answer. The figure below plots all three raw inputs (very different heights, dashed) and the single orange curve they all become after normalizing — a picture of scale- and shift-invariance. If the image fails to load: the dashed teal/plum/tan lines sit at heights around , , and respectively, while one thick orange line hits exactly for all three.

Figure s01 — Three raw rows (dashed, wildly different heights) all normalize to the one orange curve : LayerNorm keeps only the shape.

Figure s02 — For the constant row , the output is a safe for every (teal); only (orange dashed) gives the NaN cliff.


The 80/20 of the case matrix

Recall The invariances, in one breath

Adding a constant to the whole row (Cell C) or scaling it (Cell B) changes and but not LayerNorm keeps only the shape of the row. Length-2 rows collapse to (Cell E). A flat row (Cell F) — and always a single-feature row (Cell F′, ) — has , so is mandatory. The affine (Cell G) can rescale, reshape, or fully undo — no information is lost.

Which two transformations of the input leave unchanged?
Scaling every entry by and adding a constant to every entry.
For a length-2 row with , what is ?
— it keeps only the ordering of the two entries and discards their actual values.
Why does a constant row (or any row) need ?
Because gives (NaN); makes the denominator a safe positive number and the output becomes .
To fully undo LayerNorm, set and to what?
and , which recovers the original .