Layer normalization stabilizes neural-network training by normalizing each activation vector to a consistent mean and variance. It aims for mean 0 and unit variance, helping optimization converge toward effective weights. In GPT and transformer architectures, LayerNorm appears around multi-head attention and before the final output layer. section 4.2 section 4.8
For a tensor shaped like a batch of activation vectors, compute statistics across the last dimension with keepdim=True. Subtract the per-vector mean, divide by the square root of variance plus a small epsilon, then restore learned scale and shift parameters. The implementation uses unbiased=False for the variance and initializes scale to ones and shift to zeros. section 4.2 section 4.2
Normalizing a six-value activation vector
A Linear(5, 6) layer followed by ReLU produces two six-value activation vectors with nonzero means and small variances. Normalizing across each row produces approximately zero means and unit variances. The LayerNorm class then applies learned scale and shift, yielding variances close to one because epsilon is included in the denominator. section 4.2 section 4.2
Figure 4.5 An illustration of layer normalization where the six outputs of the layer, also called activations, are normalized such that they have a 0 mean and a variance of 1.
Use the illustration to follow how six layer outputs are transformed to mean 0 and variance 1. section 4.2
In the GPT pipeline, token and positional embeddings flow through repeated transformer blocks. Each block contains attention, a feed-forward network, dropout, and LayerNorm; the completed model applies a final LayerNorm before mapping hidden states to vocabulary logits. The same module also remains part of the architecture when GPT is adapted for classification. section 4.6 section 6.5






