Attention Is All You Need · Section 3.2.3

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.

Three attentions, one translation

Anna has a small dog . She likes it . → Anna hat einen kleinen Hund . Sie mag ihn .

Encoder self-attention

Encoder self-attention, layer 1, averaged over the heads Rows: the words that ask. Columns: the words they look at. Each cell is as dark as the weight, with its percentage from 10 % up; each row adds up to 100 %.Annahasasmalldog.Shelikesit.</s>Anna53 %12 %18 %has16 %16 %10 %24 %a23 %14 %15 %21 %small23 %19 %12 %11 %dog30 %14 %12 %16 %.20 %16 %17 %11 %She27 %21 %11 %likes30 %25 %it14 %21 %10 %24 %.34 %19 %23 %11 %</s>24 %21 %15 %17 %

Masked decoder self-attention

Masked decoder self-attention, layer 1, averaged over the heads Rows: the words that ask. Columns: the words they look at. Each cell is as dark as the weight, with its percentage from 10 % up; each row adds up to 100 %.<s>AnnahateinenkleinenHund.Siemagihn.<s>100 %Anna75 %25 %hat25 %75 %einen50 %50 %kleinen25 %25 %25 %25 %Hund24 %26 %50 %.50 %50 %Sie25 %50 %25 %mag75 %24 %ihn25 %50 %25 %.25 %12 %25 %38 %

Encoder–decoder attention

Encoder–decoder attention, layer 1, averaged over the heads Rows: the words that ask. Columns: the words they look at. Each cell is as dark as the weight, with its percentage from 10 % up; each row adds up to 100 %.Annahasasmalldog.Shelikesit.</s>Anna26 %58 %hat75 %25 %einen42 %18 %10 %19 %kleinen100 %Hund13 %24 %24 %25 %.25 %18 %24 %19 %Sie52 %42 %mag33 %32 %22 %ihn25 %22 %47 %.25 %35 %24 %</s>38 %54 %

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

Section 3.2.3 In plain words: In encoder–decoder attention, the decoder asks the questions and the encoder's output supplies the keys and the values.

“Each position in the encoder can attend to all positions in the previous layer of the encoder.”

Section 3.2.3 In plain words: In the encoder, every word may look at every word.

“We need to prevent leftward information flow in the decoder to preserve the auto-regressive property.”

Section 3.2.3 In plain words: The decoder must not see the words it has not written yet, so that it can write one word at a time.

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

The symbols of this section, in the paper and in our model
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 future, masked
Sentence
Playback speed

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.

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?

Your answer
A whole number

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>?

Your answer
A whole number
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.