Quant Memo
Core

Layer Normalization

A technique that rescales the activations inside a neural network layer so training stays stable, without depending on batch size the way batch normalization does.

Prerequisites: Activation Functions

Deep networks train poorly when the numbers flowing through each layer drift to very large or very small scales, because gradients then explode or vanish. Layer normalization fixes this by rescaling the activations within a single training example, one layer at a time, so each layer always sees inputs on a similar, predictable scale regardless of what came before.

Concretely, for one example's activations x=(x1,,xd)x = (x_1, \dots, x_d) in a layer, it computes the mean μ\mu and standard deviation σ\sigma across those dd values, then normalizes:

x^i=xiμσ+ϵ,yi=γx^i+β,\hat{x}_i = \frac{x_i - \mu}{\sigma + \epsilon}, \qquad y_i = \gamma \hat{x}_i + \beta,

where ϵ\epsilon is a tiny constant avoiding division by zero, and γ,β\gamma, \beta are learned scale and shift parameters that let the network undo the normalization if that turns out to help. In plain English: subtract the average and divide by the spread, then let the model relearn whatever scale it actually wants.

The key difference from batch normalization is what gets averaged over: batch norm computes μ,σ\mu,\sigma across all examples in a mini-batch (so it depends on batch size and breaks with a batch of one), while layer norm computes them across the features of one single example (so it works identically whether you process one sequence at a time or a thousand). This is why layer norm is the default in transformers and sequence models, where batch sizes vary and sequences are processed one token-position at a time.

Worked example. Given one example's pre-activation values x=(2,4,6)x = (2, 4, 6): μ=4\mu = 4, and the standard deviation is σ=(24)2+(44)2+(64)23=8/31.63\sigma = \sqrt{\frac{(2-4)^2+(4-4)^2+(6-4)^2}{3}} = \sqrt{8/3} \approx 1.63. So x^(1.22,0,1.22)\hat{x} \approx (-1.22, 0, 1.22) — same shape, now centered at zero with unit-ish spread, before γ,β\gamma,\beta rescale it.

Layer normalization rescales a single example's activations across its own features (mean 0, unit spread, then a learned rescale) rather than across a batch — which is exactly why it doesn't care about batch size and is the standard choice in transformers.

Related concepts

Practice in interviews

Further reading

  • Ba, Kiros and Hinton (2016), Layer Normalization
ShareTwitterLinkedIn