LayerNorm Normalizes Each Token Record
LayerNorm calculates mean and population variance across one token's feature coordinates, then applies learned gain and bias. Work the calculation by hand and test that tokens and batch items remain independent.
Layer normalization calculates statistics across the feature coordinates of one token record. For ,
The variance here uses denominator , not the sample-variance denominator .
Calculate One Row
Let
Its mean is . The squared centered values are
so the population variance is
Ignoring epsilon only for this hand calculation and using , gives approximately
The normalized coordinates have mean 0 and mean squared value 1, up to epsilon and rounding.
Gain and Bias Restore Learned Freedom
and each have shape . They scale and shift individual normalized features. If the first gain is 2 while the other gains remain 1, only the first normalized coordinate doubles.
LayerNorm therefore adds learned entries under the usual gain-plus-bias convention. Some implementations omit one of them; parameter counts must name the convention.
The Axis Is the Last Feature Axis
For , LayerNorm calculates a separate and for every record. It does not combine:
- two sequence positions;
- two examples in the batch;
- statistics accumulated from earlier batches.
Changing one record must not change another record's LayerNorm output. A wrong-axis implementation may keep the same outer shape while coupling tokens or batch items.
Audit LayerNorm records independently
The implementation returns statistics for every row and checks that changing one row leaves another row unchanged.
Ready to run.
Q1. Calculate LayerNorm variance
For , what population variance does LayerNorm use before adding epsilon?
Compute it first, then check your number.
Hint
Solution
Reference
- Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E. Hinton, Layer Normalization, 2016.