3.2.3 Applications of attention in the model
The same multi-head attention, used in three places: the encoder looking at itself, the decoder looking at what it has written, and the decoder looking at the encoder.
Anna has a small dog . She likes it . → Anna hat einen kleinen Hund . Sie mag ihn .
Encoder self-attention
Masked decoder self-attention
Encoder–decoder attention
Our model translating “Anna has a small dog . She likes it .”, its three kinds of attention side by side for the layer you pick, averaged over the heads. Left, the encoder's self-attention: every English word looks at every English word. Middle, the decoder's masked self-attention: each German position looks only at itself and the positions before it, so the upper right triangle is empty. Right, encoder–decoder attention: each German word being written looks at the English.
What this section says
Section 3.2.3 lists where the Transformer uses multi-head attention. The three uses differ only in where the queries, keys and values come from, and in one mask.
- Encoder–decoder attention (the decoder's middle sub-layer): the queries come from the decoder, the keys and values from the encoder's output. Every position of the output can look at every word of the input; this is how the German finds the English it translates.
- The encoder's self-attention: queries, keys and values all come from the previous encoder layer. Every English word may look at every English word, before and after it.
- The decoder's masked self-attention: each position may look at itself and the positions before it, never after. The scores for later positions are set to −∞ before the softmax, so their weights are exactly 0 (the mask of Figure 2).
Why the mask: in training, the decoder sees the whole German translation at once, shifted one place to the right, and predicts every next word in parallel. Without the mask, position i could simply read the word it is meant to predict. With it, training matches what happens when translating, where the later words do not exist yet.
In the paper's words
“In "encoder-decoder attention" layers, the queries come from the previous decoder layer, and the memory keys and values come from the output of the encoder.”
“Each position in the encoder can attend to all positions in the previous layer of the encoder.”
“We need to prevent leftward information flow in the decoder to preserve the auto-regressive property.”
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 |
|---|---|---|---|
| \(Q,\ K,\ V\) | where each comes from decides the kind of attention | as listed above | the same |
| \(-\infty\) | the score given to a forbidden position before the softmax; its weight becomes e^{−∞} = 0 | decoder self-attention only | the same |
| \(i\) | a position in the decoder: it may look at positions 1 to i | any | any |
Try it
The grid of scores of one head of the GPT-style model on the chosen sentence: one row for each word that asks, one column for each word it could look at. First every score, as the model would compute it; then every cell to the right of the diagonal, the future, is greyed out and set to −∞; then the softmax runs row by row, the future becomes exactly 0 and each row's weights add up to 1.
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.
Queries from one side, keys from the other
In which kind of attention do the queries come from the decoder and the keys and values from the encoder?
What the fourth position sees
In the decoder's masked self-attention, how many positions can the fourth position (counting <s> as the first) give any weight to?
What “it” sees in the encoder
In the encoder's self-attention on “Anna has a small dog . She likes it .”, how many positions can “it” look at, counting the end mark </s>?