14. Residual connections and layer normalisation
Every sub-layer of the transformer adds its result to what came in, rather than replacing it, and then rescales the sum to a standard size. Those two habits are what let the paper stack layer on layer.
One word's vector x in the encoder's first layer, drawn as 64 bars, and the attention sub-layer's output for it. The two take separate paths, x around the sub-layer like a bypass, and are added. Layer normalisation then subtracts the sum's mean, divides by its spread, and applies the learned γ and β: the result is exactly the model's output. Last, dropout, as in training only: about one value in ten of the sub-layer's output is switched off before the add.
The idea
Each sub-layer of the transformer, an attention or a feed-forward network, does not replace the word's vector with its own result. It adds its result to the vector it was given. That path around the sub-layer is the residual connection: the original information always gets through, and each sub-layer only has to learn a correction. If a sub-layer has nothing useful to add, it can learn to add almost nothing.
After every addition comes layer normalisation. It takes the word's 64 numbers, subtracts their mean so they centre on 0, and divides by their spread so they have a variance of 1. Then it multiplies each number by a learned γ and adds a learned β, so the model can choose the size it actually wants. Without this, numbers would grow or shrink a little in every layer, and after a few layers they would be useless.
While the model trains, one more thing happens on the way: dropout. Before the add, a random tenth of the sub-layer's output is switched off, so the model cannot lean on any single number. When it translates, dropout is off.
A worked example: “it” in the first layer
In “Anna has a small dog . She likes it .”, the vector x of “it” begins [0.89, 0.04, 0.83, 0.88, …], and the attention sub-layer's output for it begins [−0.58, 0.17, −0.13, −0.39, …]. Added: [0.31, 0.21, 0.70, 0.49, …].
The 64 numbers of the sum have a mean of 0.106 and a variance of 0.511. Subtract the mean and divide by √(0.511 + ε): the numbers now begin [0.28, 0.14, 0.83, 0.54, …], with a mean of 0.000 and a variance of 1.000.
γ begins [1.09, 1.02, 1.10, 1.09, …] and β begins [−0.20, −0.22, −0.17, −0.16, …]. Multiplied and added: [0.11, −0.07, 0.74, 0.42, …], exactly what the model passes on to its feed-forward network.
The formula
- \(x\)
- the word's vector coming into the sub-layer
- \(\operatorname{Sublayer}(x)\)
- what the sub-layer computes: multi-head attention or the feed-forward network
- \(x + \operatorname{Sublayer}(x)\)
- the residual connection: the input added back to the output
- \(\mu,\ \sigma^2\)
- the mean and the variance of that word's d_model numbers
- \(\epsilon\)
- a tiny number, 10⁻⁵ in our model, so the division never divides by 0
- \(\gamma,\ \beta\)
- learned per dimension: the size and the centre the model wants
- \(\odot\)
- number by number
Dropout, during training, sits on the sub-layer's output before the add: each value is set to 0 with probability \(P_{drop}\) and the rest are divided by \(1 - P_{drop}\), so on average nothing changes. The paper's \(P_{drop}\) is 0.1.
Why it matters
The residual path is why transformers can be deep. Each layer only nudges the vector, so stacking many layers (large published models have dozens, where the paper has six) does not drown the original word; and while training, the error signal can flow straight back down the bypass to the first layers.
Normalisation is why it trains at all at that depth: every layer receives numbers of the same scale, whatever happened below it. When you read that a model “diverged” in training, numbers growing out of control is usually what happened, and normalisation is one of the guards against it.
Where it fails
- Where the normalisation sits is a choice. The paper normalises after the add (post-norm), which our model copies, and trains with a learning-rate warm-up (chapter 20). GPT-style models moved it before the sub-layer (pre-norm, chapter 22).
- Normalisation erases size. After LayerNorm every word's vector has the same spread, so “how strongly” has to be carried by the pattern of the numbers, not by their size.
Dropout is a trade. It makes every training step noisier, and it pays off only if the model would otherwise learn its examples by heart. The paper's Table 3 (row D) shows its model worse without it. In our reruns of row D, over three seeds, the held-out loss at the end is 0.114 without dropout against 0.063 with it: on our small task it helps too.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
Normalise by hand
Layer normalisation of the four numbers 2, 4, 6, 8 (γ = 1, β = 0): what does the first number become? Two decimals.
The mean before normalising
In the clip's first sentence, what is the mean of the 64 numbers of x + Sublayer(x) for “it”? Two decimals.
How many switched off?
In the clip's first sentence, how many of the 64 values of Sublayer(x) does the clip's dropout mask switch off?
When is dropout on?
When does the paper's model use dropout?
In the paper
Section 3.1: “We employ a residual connection around each of the two sub-layers, followed by layer normalization. That is, the output of each sub-layer is LayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer itself.” All sub-layers and the embeddings produce vectors of d_model = 512 numbers so that the additions work. Section 5.4 adds dropout: “We apply dropout to the output of each sub-layer, before it is added to the sub-layer input and normalized”, with \(P_{drop} = 0.1\) for the base model.
In the paper: section 3.1, 5.4