3.1 Encoder and decoder stacks
What is inside each half of Figure 1: identical layers stacked N times, every sub-layer wrapped in a residual connection and layer normalisation.
Encoder layer 1 of 2: “it” looks most at “it” (24.4 %, averaged over the four heads), and the layer changes its vector by 77.8 %.
| Token | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … 56 more |
|---|---|---|---|---|---|---|---|---|---|
| it | 0.22 | −0.39 | 0.93 | −0.07 | −0.41 | −1.40 | 0.02 | 0.73 | … |
First one encoder layer, then the encoder's N layers, then the decoder's, each layer a button. Your sentence lights the layer you pick: for each word, the word it looks at most, how much the layer changed its vector, and the first numbers of the vector it passes up.
What this section says
The encoder is N identical layers, 6 in the paper. Each has two sub-layers: multi-head self-attention, then a small feed-forward network applied to every position. Identical means the same design, not the same numbers: each layer learns its own weights.
Around each sub-layer sits a residual connection followed by layer normalisation: the sub-layer's input x is added back to its output, and the sum is rescaled. The paper writes it as one formula, LayerNorm(x + Sublayer(x)). To make the addition possible, every sub-layer and the embeddings produce vectors of the same size, d_model.
The decoder is also N identical layers, with a third sub-layer in the middle: attention over the encoder's output (section 3.2.3). Its self-attention is masked so that a position cannot look at later positions. Together with feeding the decoder its own output shifted one place to the right, this means the prediction for position i depends only on the tokens before i.
In the paper's words
“The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network.”
“We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.”
Quoted from Vaswani et al., Attention Is All You Need, arXiv:1706.03762v7(opens in a new tab). The figures on this page are redrawn, not copied.
The notation
| Symbol | Meaning | In the paper | Our miniature |
|---|---|---|---|
| \(N\) | identical layers in each stack | 6 | 2 |
| \(d_{\text{model}}\) | numbers in every vector between sub-layers, and in the embeddings | 512 | 64 |
| \(x\) | a sub-layer's input: one vector per token | n × 512 | n × 64 |
| \(\mathrm{LayerNorm}(x + \mathrm{Sublayer}(x))\) | the output of every sub-layer: the residual sum, normalised | after every sub-layer (post-normalisation) | the same |
Try it
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.
How our miniature differs
Our stacks have N = 2 layers of vectors of 64 numbers. The layers hold 231,936 of our model's 244,800 weights; the rest is the one shared embedding matrix (section 3.4). Today's GPT-style models usually normalise before each sub-layer instead of after it; the paper and our miniature normalise after.
Prepared in the Course by
Check yourself
Three questions a reader of this section should now be able to answer. The lab checks your answer against the paper and our models.
Counting sub-layers
How many sub-layers does one decoder layer have?
The decoder's extra sub-layer
What does the decoder's third sub-layer do?
All the paper's sub-layers
In the paper's base model (N = 6), how many sub-layers are there in the encoder and the decoder together?