liveBook · Manning

Layer normalization

As explained in Build a Large Language Model (From Scratch)

A normalization technique that stabilizes neural network training by normalizing activations to have more consistent statistics across layers.

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.
figure

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

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains per-vector statistics, epsilon, trainable scale and shift, tensor dimensions, and a PyTorch implementation.
Examples
Demonstrates normalization on two six-value ReLU activation vectors and verifies their means and variances with PyTorch.
What it assumes
Assumes basic neural-network tensors and activations while treating deep gradient mathematics as unnecessary.
What you can do after
Build it from scratch as a reusable PyTorch LayerNorm module and insert it into a GPTModel.
How it connects
Connects layer normalization to transformer blocks, multi-head attention, feed-forward networks, dropout, embeddings, and GPT classification heads.

Also explained in

Compare how 7 books explain layer normalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.