Interpret per-token layer normalization

Type the indices of each value in their original order. · 176 words

Layer normalization (LayerNorm) rescales each token's hidden vector separately. It does not mix tokens. For z = [2, -2, 0], the mean is (2 - 2 + 0) / 3 = 0. The squared deviations are [4, 4, 0]. Their average, called the population variance, is 8 / 3, and its square root is the standard deviation, about 1.633. Ignoring the tiny epsilon used in code, divide each centered coordinate by 1.633 to get about [1.225, -1.225, 0]. This token now has mean 0 and variance 1.

Real transformers usually learn a scale gamma and shift beta after normalization. With gamma = [1.5, 0.5, 2] and beta = [0.1, -0.1, 0], the final value is about [1.937, -0.712, 0]. LayerNorm compares a token only with its own coordinates. For example, [10, 10, 10] normalizes to [0, 0, 0] because its own mean is 10. A neighboring token never affects this result. This differs from batch normalization, which uses batch statistics. A transformer may place LayerNorm before attention or before its feed-forward block, depending on its design.

Check your recall

Using population variance and no epsilon, what is the normalized vector for the token [2, -2, 0]?

Check your answer

Its mean is 0 and its standard deviation is sqrt(8 / 3), so the normalized vector is about [1.225, -1.225, 0].

Sources and credits

Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.