Layer Normalization
Layer normalization (LayerNorm) standardizes the activations of one example using statistics computed within that example. For an example whose layer holds the values , it computes
and outputs
The values depend on the kind of layer:
- In a convolutional layer they are all activations of the example, so the statistics run over every channel and every position. The learned scale and offset are shared by all positions of a channel, so and depend only on the channel that holds value .
- In a transformer they are the features of one token, and each token is normalized on its own.
The standard deviation is the root-mean-square norm of the centered vector .
LayerNorm does not depend on the batch. It computes the same function during training and at test time, works with a batch of one, and needs no stored statistics. It is the standard normalization in transformers and recurrent networks. Unlike batch normalization, it adds no noise from the batch, so it lacks that source of regularization.
Intuition
Batch normalization asks how an activation compares with the same activation in other examples. Layer normalization asks how it compares with the other activations of the same example. The second question can always be answered, whatever the batch looks like, which is why LayerNorm took over in sequence models. A sentence of any length can be normalized one token at a time, and the computation at test time is the same as during training.
The price is what the normalization erases. LayerNorm removes the overall level and spread of each example’s activations. If one input produces a layer whose activations are all three times those of another, the two become identical after LayerNorm. Batch normalization would keep that difference, since it compares the two examples feature by feature.
Properties
(No Batch Coupling) Each example is normalized independently, so the layer behaves identically for every batch size and at test time.
Batch normalization’s output for one example depends on the other examples in the batch, and its test-time behavior depends on frozen statistics. LayerNorm has neither dependence. It works with a batch of one, with batches split across machines, and with sequences of any length. Each token of a sequence is normalized with its own statistics, so nothing has to be stored per time step. Training and testing use the same formula, so there is no train-test mismatch to manage.
(What It Removes) LayerNorm makes a layer’s output invariant to shifting or rescaling all of its input activations together.
If every of an example is replaced by with , the mean becomes , the spread becomes , and the standardized values do not change. The overall magnitude of a layer’s response to an input is therefore lost after the layer. This is useful when that magnitude is a nuisance, such as the length of a token vector growing through many residual blocks. It is harmful when the magnitude carries meaning that later layers need. The learned and cannot restore it, because they are the same for every example.
(Placement in Transformers) Modern transformers apply LayerNorm at the start of each residual block.
Not Taught in Lecture
Lecture introduced the pre-norm convention for residual blocks but had not yet covered transformers when this note was written.
In a pre-norm block the input of the attention or feedforward branch is normalized, and the branch’s output is added to the unnormalized input. This keeps a clean identity path through the residual connections, as in pre-activation ResNets. The original transformer applied LayerNorm after each addition instead, which is called post-norm.
Examples
(Easy) Normalizing a Token
One token has the feature vector , and its LayerNorm has and .
Compute the output. . The centered vector is , so . The output is . Any other token whose features are with gives the same output.
The mean is
(Medium) LayerNorm Versus BatchNorm on the Same Batch
A batch of two examples with three features each is stored as the rows of
so the second example is three times the first.Normalize the batch with LayerNorm and with BatchNorm, using , and negligible . What does each one keep? LayerNorm normalizes each row. The first row has mean and standard deviation . The second has mean and standard deviation . Both become . BatchNorm normalizes each column. Every column has the form , with mean and standard deviation , so every column becomes : LayerNorm keeps the pattern across features within each example and erases the fact that the second example is larger. BatchNorm keeps which example is larger for each feature and erases the pattern across features. The same batch carries different information after each layer.
See Also
- 4.5. Neural Network Normalization, which compares LayerNorm with the other normalization layers.
- 4.5.3. Group Normalization, which normalizes over groups of channels and reduces to LayerNorm with a single group.
- 7.2. Transformer Architecture, where LayerNorm is part of every block.