24. Translate a sentence end to end
Everything in one run: your English sentence through the paper's model, every step of the trace in order, each linked to the chapter that taught it. Then three experiments on the real model, each predicted before it runs.
Anna has a small dog . She likes it . → Anna hat einen kleinen Hund . Sie mag ihn . · 211 steps Play it on Figure 1
The encoder reads the English · 12 steps
Encoder · Input sentence → tokens11 tokens: Anna has a small dog . She likes it . </s>
The English sentence is split into tokens, and each token is looked up in the vocabulary the two languages share, which gives it a number. The token </s> is added to mark where the sentence ends (section 3.1).
Encoder · Input Embedding“it” becomes 64 numbers; the first is −0.10.
Each token's number picks its row of the embedding table: 64 numbers learned in training. The paper multiplies each row by √d_model, here √64 = 8 (section 3.4).
Encoder · Positional EncodingPositions added to 11 rows.
Attention on its own cannot tell word order, so each position adds its own pattern of sine and cosine waves to the token's row. The sum is what the first layer reads (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Encoder layer 1 · Multi-head self-attention“it” looks most at “it” (24.4 %, the average of the 4 heads).
Every English token looks at every token, itself included, through 4 heads at once. Each head scores how well a token's query matches every key, divides by √d_k = 4, and the softmax turns each row of scores into weights that add up to 1; the weights mix the values. The heads' results are joined and multiplied by W^O (sections 3.2.1 and 3.2.2).
Chapter 11: Multi-head attention Theory: 3.2.1 Scaled dot-product attention
Encoder layer 1 · Add & Norm“it”: mean 0.106 and variance 0.511 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Encoder layer 1 · Feed forward247 of 256 values became zero for “it”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Encoder layer 1 · Add & Norm“it”: mean 0.089 and variance 4.249 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Encoder layer 2 · Multi-head self-attention“it” looks most at “dog” (32.1 %, the average of the 4 heads).
Every English token looks at every token, itself included, through 4 heads at once. Each head scores how well a token's query matches every key, divides by √d_k = 4, and the softmax turns each row of scores into weights that add up to 1; the weights mix the values. The heads' results are joined and multiplied by W^O (sections 3.2.1 and 3.2.2).
Chapter 11: Multi-head attention Theory: 3.2.1 Scaled dot-product attention
Encoder layer 2 · Add & Norm“it”: mean 0.006 and variance 1.251 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Encoder layer 2 · Feed forward255 of 256 values became zero for “it”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Encoder layer 2 · Add & Norm“it”: mean 0.035 and variance 1.666 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Encoder output: the memory the decoder reads11 vectors go to the decoder.
After the last encoder layer, these vectors are the encoder's output. The decoder never reads the English words again, only these vectors: every encoder–decoder attention takes its keys and values from them (section 3.2.3).
Chapter 17: The encoder Theory: 3.2.3 Applications of attention in the model
The decoder writes word 1: “Anna” · 18 steps
Decoder · Outputs (shifted right) · writing word 1The decoder reads: <s>
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 1“<s>” becomes 64 numbers; the first is −0.77.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 1Positions added to 1 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 1“<s>” looks most at “<s>” (100.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 1“<s>”: mean 0.003 and variance 1.614 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 1The word being written, word 1, looks most at “has” (57.9 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 1“<s>”: mean −0.006 and variance 2.665 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 1241 of 256 values became zero for “<s>”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 1“<s>”: mean 0.019 and variance 13.521 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 1“<s>” looks most at “<s>” (100.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 1“<s>”: mean 0.019 and variance 0.954 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 1The word being written, word 1, looks most at “Anna” (99.8 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 1“<s>”: mean 0.229 and variance 10.100 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 1241 of 256 values became zero for “<s>”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 1“<s>”: mean 0.121 and variance 8.997 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 1The highest score is “Anna”, 7.79.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 1“Anna” has probability 92.4 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 1 chosen: “Anna”It writes “Anna” (92.4 %). So far: Anna
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 2: “hat” · 18 steps
Decoder · Outputs (shifted right) · writing word 2The decoder reads: <s> Anna
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 2“Anna” becomes 64 numbers; the first is −2.33.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 2Positions added to 2 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 2“Anna” looks most at “<s>” (75.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 2“Anna”: mean 0.318 and variance 4.723 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 2The word being written, word 2, looks most at “has” (75.0 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 2“Anna”: mean −0.074 and variance 3.272 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 2227 of 256 values became zero for “Anna”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 2“Anna”: mean −0.016 and variance 25.452 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 2“Anna” looks most at “<s>” (80.9 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 2“Anna”: mean 0.003 and variance 1.125 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 2The word being written, word 2, looks most at “has” (99.8 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 2“Anna”: mean 0.024 and variance 7.655 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 2244 of 256 values became zero for “Anna”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 2“Anna”: mean 0.007 and variance 5.466 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 2The highest score is “hat”, 7.89.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 2“hat” has probability 92.7 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 2 chosen: “hat”It writes “hat” (92.7 %). So far: Anna hat
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 3: “einen” · 18 steps
Decoder · Outputs (shifted right) · writing word 3The decoder reads: <s> Anna hat
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 3“hat” becomes 64 numbers; the first is −1.99.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 3Positions added to 3 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 3“hat” looks most at “hat” (75.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 3“hat”: mean 0.325 and variance 4.891 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 3The word being written, word 3, looks most at “a” (42.3 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 3“hat”: mean −0.002 and variance 2.649 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 3218 of 256 values became zero for “hat”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 3“hat”: mean −0.312 and variance 65.603 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 3“hat” looks most at “hat” (45.3 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 3“hat”: mean −0.015 and variance 1.128 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 3The word being written, word 3, looks most at “a” (73.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 3“hat”: mean −0.251 and variance 9.048 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 3228 of 256 values became zero for “hat”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 3“hat”: mean −0.154 and variance 17.670 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 3The highest score is “einen”, 8.07.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 3“einen” has probability 94.0 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 3 chosen: “einen”It writes “einen” (94.0 %). So far: Anna hat einen
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 4: “kleinen” · 18 steps
Decoder · Outputs (shifted right) · writing word 4The decoder reads: <s> Anna hat einen
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 4“einen” becomes 64 numbers; the first is 0.19.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 4Positions added to 4 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 4“einen” looks most at “Anna” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 4“einen”: mean 0.478 and variance 6.753 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 4The word being written, word 4, looks most at “small” (99.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 4“einen”: mean 0.059 and variance 2.428 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 4225 of 256 values became zero for “einen”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 4“einen”: mean −0.013 and variance 21.067 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 4“einen” looks most at “<s>” (49.5 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 4“einen”: mean −0.055 and variance 1.659 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 4The word being written, word 4, looks most at “small” (99.9 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 4“einen”: mean −0.170 and variance 6.991 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 4233 of 256 values became zero for “einen”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 4“einen”: mean −0.045 and variance 6.050 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 4The highest score is “kleinen”, 8.30.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 4“kleinen” has probability 94.7 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 4 chosen: “kleinen”It writes “kleinen” (94.7 %). So far: Anna hat einen kleinen
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 5: “Hund” · 18 steps
Decoder · Outputs (shifted right) · writing word 5The decoder reads: <s> Anna hat einen kleinen
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 5“kleinen” becomes 64 numbers; the first is −0.39.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 5Positions added to 5 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 5“kleinen” looks most at “Anna” (25.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 5“kleinen”: mean 0.464 and variance 4.656 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 5The word being written, word 5, looks most at “</s>” (25.4 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 5“kleinen”: mean 0.008 and variance 2.405 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 5199 of 256 values became zero for “kleinen”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 5“kleinen”: mean 0.058 and variance 66.203 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 5“kleinen” looks most at “einen” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 5“kleinen”: mean −0.040 and variance 3.117 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 5The word being written, word 5, looks most at “dog” (100.0 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 5“kleinen”: mean −0.092 and variance 6.720 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 5234 of 256 values became zero for “kleinen”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 5“kleinen”: mean −0.028 and variance 7.166 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 5The highest score is “Hund”, 8.03.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 5“Hund” has probability 94.0 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 5 chosen: “Hund”It writes “Hund” (94.0 %). So far: Anna hat einen kleinen Hund
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 6: “.” · 18 steps
Decoder · Outputs (shifted right) · writing word 6The decoder reads: <s> Anna hat einen kleinen Hund
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 6“Hund” becomes 64 numbers; the first is 0.98.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 6Positions added to 6 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 6“Hund” looks most at “einen” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 6“Hund”: mean 0.154 and variance 5.585 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 6The word being written, word 6, looks most at “.” (25.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 6“Hund”: mean −0.006 and variance 1.790 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 6215 of 256 values became zero for “Hund”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 6“Hund”: mean 0.166 and variance 76.154 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 6“Hund” looks most at “einen” (75.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 6“Hund”: mean 0.017 and variance 4.238 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 6The word being written, word 6, looks most at “.” (51.1 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 6“Hund”: mean −0.039 and variance 6.757 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 6245 of 256 values became zero for “Hund”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 6“Hund”: mean 0.039 and variance 2.916 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 6The highest score is “.”, 7.60.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 6“.” has probability 91.1 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 6 chosen: “.”It writes “.” (91.1 %). So far: Anna hat einen kleinen Hund .
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 7: “Sie” · 18 steps
Decoder · Outputs (shifted right) · writing word 7The decoder reads: <s> Anna hat einen kleinen Hund .
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 7“.” becomes 64 numbers; the first is 0.50.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 7Positions added to 7 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 7“.” looks most at “.” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 7“.”: mean −0.074 and variance 4.432 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 7The word being written, word 7, looks most at “She” (52.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 7“.”: mean −0.030 and variance 1.837 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 7232 of 256 values became zero for “.”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 7“.”: mean −0.270 and variance 32.410 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 7“.” looks most at “Hund” (49.7 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 7“.”: mean 0.017 and variance 2.549 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 7The word being written, word 7, looks most at “She” (75.0 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 7“.”: mean 0.106 and variance 4.835 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 7243 of 256 values became zero for “.”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 7“.”: mean −0.012 and variance 8.398 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 7The highest score is “Sie”, 8.06.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 7“Sie” has probability 93.6 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 7 chosen: “Sie”It writes “Sie” (93.6 %). So far: Anna hat einen kleinen Hund . Sie
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 8: “mag” · 18 steps
Decoder · Outputs (shifted right) · writing word 8The decoder reads: <s> Anna hat einen kleinen Hund . Sie
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 8“Sie” becomes 64 numbers; the first is −1.35.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 8Positions added to 8 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 8“Sie” looks most at “.” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 8“Sie”: mean −0.132 and variance 11.049 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 8The word being written, word 8, looks most at “She” (32.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 8“Sie”: mean −0.053 and variance 2.154 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 8227 of 256 values became zero for “Sie”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 8“Sie”: mean −0.003 and variance 54.662 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 8“Sie” looks most at “Hund” (46.8 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 8“Sie”: mean 0.031 and variance 1.716 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 8The word being written, word 8, looks most at “likes” (99.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 8“Sie”: mean −0.001 and variance 10.498 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 8239 of 256 values became zero for “Sie”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 8“Sie”: mean −0.012 and variance 6.386 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 8The highest score is “mag”, 8.00.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 8“mag” has probability 93.3 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 8 chosen: “mag”It writes “mag” (93.3 %). So far: Anna hat einen kleinen Hund . Sie mag
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 9: “ihn” · 18 steps
Decoder · Outputs (shifted right) · writing word 9The decoder reads: <s> Anna hat einen kleinen Hund . Sie mag
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 9“mag” becomes 64 numbers; the first is 0.03.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 9Positions added to 9 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 9“mag” looks most at “.” (75.1 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 9“mag”: mean 0.028 and variance 6.830 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 9The word being written, word 9, looks most at “it” (47.0 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 9“mag”: mean 0.025 and variance 2.604 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 9231 of 256 values became zero for “mag”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 9“mag”: mean −0.096 and variance 26.122 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 9“mag” looks most at “einen” (25.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 9“mag”: mean 0.053 and variance 2.615 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 9The word being written, word 9, looks most at “it” (45.9 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 9“mag”: mean −0.163 and variance 5.133 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 9236 of 256 values became zero for “mag”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 9“mag”: mean −0.082 and variance 7.380 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 9The highest score is “ihn”, 8.02.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 9“ihn” has probability 94.2 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 9 chosen: “ihn”It writes “ihn” (94.2 %). So far: Anna hat einen kleinen Hund . Sie mag ihn
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 10: “.” · 18 steps
Decoder · Outputs (shifted right) · writing word 10The decoder reads: <s> Anna hat einen kleinen Hund . Sie mag ihn
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 10“ihn” becomes 64 numbers; the first is −0.14.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 10Positions added to 10 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 10“ihn” looks most at “mag” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 10“ihn”: mean 0.384 and variance 6.893 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 10The word being written, word 10, looks most at “She” (35.2 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 10“ihn”: mean −0.014 and variance 1.757 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 10209 of 256 values became zero for “ihn”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 10“ihn”: mean 0.331 and variance 112.293 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 10“ihn” looks most at “einen” (65.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 10“ihn”: mean 0.012 and variance 4.093 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 10The word being written, word 10, looks most at “.” (51.5 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 10“ihn”: mean −0.039 and variance 6.770 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 10245 of 256 values became zero for “ihn”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 10“ihn”: mean 0.039 and variance 2.886 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 10The highest score is “.”, 7.58.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 10“.” has probability 91.0 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 10 chosen: “.”It writes “.” (91.0 %). So far: Anna hat einen kleinen Hund . Sie mag ihn .
The word with the highest probability is written and fed back in as the decoder's next input. This walkthrough follows that choice, greedy search; the paper searched with a beam of 4 (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The decoder writes word 11: “</s>” · 18 steps
Decoder · Outputs (shifted right) · writing word 11The decoder reads: <s> Anna hat einen kleinen Hund . Sie mag ihn .
The decoder writes one word at a time. Its input is what it has written so far, shifted one place to the right: it starts with <s>, so at each position it predicts the word that comes next (section 3.1).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.1 Encoder and decoder stacks
Decoder · Output Embedding · writing word 11“.” becomes 64 numbers; the first is 0.50.
The German tokens are looked up in the same embedding table as the English, because the paper shares one matrix, and multiplied by √64 = 8 (section 3.4).
Decoder · Positional Encoding · writing word 11Positions added to 11 rows.
The same sine and cosine waves are added for each German position (section 3.5).
Chapter 12: Positions as sine waves Theory: 3.5 Positional encoding
Decoder layer 1 · Masked multi-head self-attention · writing word 11“.” looks most at “.” (38.5 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 11“.”: mean −0.237 and variance 4.730 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Encoder–decoder attention · writing word 11The word being written, word 11, looks most at “</s>” (54.4 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 1 · Add & Norm · writing word 11“.”: mean −0.021 and variance 1.624 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 1 · Feed forward · writing word 11223 of 256 values became zero for “.”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 1 · Add & Norm · writing word 11“.”: mean −0.516 and variance 74.376 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Masked multi-head self-attention · writing word 11“.” looks most at “mag” (50.0 %, the average of the 4 heads).
Each German position looks at itself and the words before it, never after: the scores of later positions are set to −∞ before the softmax, so their weights are 0. When translating, those words do not exist yet (section 3.2.3).
Chapter 10: Masking Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 11“.”: mean −0.060 and variance 3.414 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Encoder–decoder attention · writing word 11The word being written, word 11, looks most at “</s>” (49.9 %, the average of the 4 heads).
Encoder–decoder attention: the German position asks with its query, and the keys and values come from the encoder's output. This is where the decoder looks at the English (section 3.2.3).
Chapter 18: The decoder and encoder–decoder attention Theory: 3.2.3 Applications of attention in the model
Decoder layer 2 · Add & Norm · writing word 11“.”: mean −0.055 and variance 6.003 before normalising.
The attention's result is added to its input (the residual connection). Then each token's 64 numbers are re-centred to mean 0 and scaled to variance 1, and a learned gain and bias are applied (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder layer 2 · Feed forward · writing word 11246 of 256 values became zero for “.”.
Each position on its own goes through two small layers: its 64 numbers are widened to 256, every negative one is set to zero by the ReLU, and the result is narrowed back to 64. Every position uses the same weights (section 3.3).
Chapter 13: The position-wise feed-forward network Theory: 3.3 Position-wise feed-forward networks
Decoder layer 2 · Add & Norm · writing word 11“.”: mean −0.032 and variance 2.965 before normalising.
The feed-forward result is added to its input and normalised the same way. This is the layer's output, which the next layer reads (section 3.1).
Chapter 14: Residual connections and layer normalisation Theory: 3.1 Encoder and decoder stacks
Decoder · Linear · writing word 11The highest score is “</s>”, 7.66.
The newest position's 64 numbers are compared with every word's row of the same shared embedding matrix: one score, a logit, for each word of the vocabulary (section 3.4).
Chapter 15: Shared embeddings and the output Theory: 3.4 Embeddings and softmax
Decoder · Softmax · writing word 11“</s>” has probability 91.6 %.
The softmax turns the scores into probabilities that add up to 1 (section 3.4).
Chapter 6: Softmax: scores into probabilities Theory: 3.4 Embeddings and softmax
Word 11 chosen: “</s>”It writes “</s>” (91.6 %). So far: Anna hat einen kleinen Hund . Sie mag ihn .
The model rates </s> highest: the translation is finished (section 6.1).
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
The translation · 1 step
The finished translationTranslation: Anna hat einen kleinen Hund . Sie mag ihn .. Beam search gives the same.
The walkthrough followed the word the model rates highest at each step (greedy search). The paper used beam search with 4 beams (section 6.1); for this sentence, beam search gives the same translation.
Chapter 19: Translating: greedy search and beam search Theory: 6.1 Machine translation
Type an English sentence or pick one. The paper's model translates it, and every step of the forward pass is listed in order: the encoder, then one group for each German word, then the translation. Open any step to see what happens, the step's own numbers, and links to its chapter and to the paper's section in the Theory tab.
The idea
Chapters 2 to 21 each took one piece of the paper's model. Here they run together, in the order the model computes them, on a sentence you choose: tokens and embeddings (chapters 2 and 5), sine positions (12), the encoder's self-attention, Add & Norm and feed-forward layers (11, 14, 13), then the decoder writing one word at a time with masked self-attention (10) and encoder–decoder attention (18), the shared output matrix and the softmax (15, 6), and the choice of each word (19).
Every number in the trace comes from the engine running the real model, the same numbers the translation walkthrough plays on Figure 1. The trace follows greedy search, the most likely word each time; the last group says whether beam search would choose differently.
An experiment on a model changes one thing and runs it again. Predict the result first: a prediction that turns out wrong teaches more than a result read passively. Removing a part to see what it did is called an ablation, and the paper's Table 3 is a list of them.
A worked example: one sentence end to end
For “Anna has a small dog . She likes it .”, the trace has 211 steps: 12 in the encoder, then 18 for each of the 11 words the decoder writes (the German and the closing </s>), and the finished translation: “Anna hat einen kleinen Hund . Sie mag ihn .”.
Remove encoder–decoder head 2.1 of the decoder's second layer, the head that puts its weight on “dog” when writing “ihn” (chapter 18), and run the whole model again from the start: the German is “Anna hat einen kleinen Hund . Sie mag ihn .”. The largest change is to “mag”, whose probability falls by 0.16 percentage points.
The other heads carry the same information: in our own check, removing any one of the 24 heads alone changes none of the 805 held-out translations. The paper found the same kind of redundancy in Table 3: one head instead of eight costs it less than one BLEU point.
Three experiments: predict, then run
Anna has a small dog . She likes it .
1. Remove a head
With this head removed from the whole forward pass, will the German change?
2. Learned positions instead of sines
In our Table 3 rerun, the model was trained again with learned positions instead of sine waves. Which first translated 95 % of the held-out sentences?
3. A beam of one
With a beam of one, which is greedy search, will the German for your sentence be the same as with the paper's beam of 4?
Three experiments on the paper's model. For each, choose your prediction first; then run it. Removing a head reruns the whole forward pass with that head's output set to zero; learned positions show our recorded rerun of Table 3, row E, a separately trained model; a beam of one compares greedy search with the paper's beam of 4 on your sentence.
Learned positions cannot be switched on in the model you just traced: the model was trained with sine waves and its weights expect them. What the lab has is a second model trained with learned positions in our Table 3 reruns, 3 seeds each. Its record shows when it learned, not how it translates your sentence: it reached 95 % of the held-out sentences at step 193 on average (from 180 to 200), the sines at step 187 (from 180 to 200).
The formula
- \(x\)
- the English sentence, read by the encoder
- \(y_{<t}\)
- the German written so far, read by the decoder
- \(h_t^{(N)}\)
- the decoder's output vector at position t, after the last of its N layers
- \(E^{T}\)
- the shared embedding matrix, turned to score every word
- \(\mathrm{head}_j\)
- the head removed: its output is replaced by zeros before W^O, and every later step is computed again
Why it matters
A trace is how you debug a model: when an answer is wrong, you follow it back step by step to where it went wrong, as you would follow a request through a distributed system. Interpretability research does this with real models, and ablations like these are among its basic tools.
The head experiment also says something about production models: their parts overlap. A single component rarely decides an answer on its own, which makes models robust and also makes "which part is responsible" a hard question.
Where it fails
- A tiny model on an easy task. Our model is so sure of itself that removing a head barely moves it; on a hard task, or in a model with fewer spare heads, one head can matter more.
- Learned positions are a different model. The lab can show the rerun's training record, not its translation of your sentence; and the comparison is on a task far easier than the paper's.
- Greedy and beam search agree almost always here. Our confident model rarely needs the beam; chapter 19 shows the one held-out sentence where they differ, and why.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
Counting the trace
How many steps does the trace of “Anna has a small dog . She likes it .” have?
One word, how many steps?
How many steps does the decoder take for each word it writes, with our 2 layers?
The pronoun head removed
With encoder–decoder head 2.1 removed from the whole forward pass, does the translation of “Anna has a small dog . She likes it .” change?
Learned positions, recorded
At which step, on average over the seeds, did our rerun with learned positions first translate 95 % of the held-out sentences?
In the paper
Section 3 describes the run you just traced: “At each step the model is auto-regressive [10], consuming the previously generated symbols as additional input when generating the next.” Section 6.2 removes parts as the experiments do: “While single-head attention is 0.9 BLEU worse than the best setting, quality also drops off with too many heads.”
In the paper: section 3, 6.2