Attention Is All You Need · Appendix: attention visualisations

Appendix: attention visualisations

The paper's last pages show what some heads attend to. We draw the same kind of picture for our model, and you can find the head that links a pronoun to its noun.

One word's attention, head by head
Word
Encoder self-attention of “it” in layer 1, one colour per headAnna Annahas hasa asmall smalldog dog. .She Shelikes likesit it. .</s> </s>
  • Head 1: “it” → “Anna” (52 %)
  • Head 2: “it” → “has” (49 %)
  • Head 3: “it” → “has” (24 %)
  • Head 4: “it” → “it” (38 %)

Our model's encoder self-attention drawn as the paper's appendix draws it: the sentence twice, one row above the other, and lines from the chosen word above to every word below, one colour per head, each line as strong as its weight. Pick the layer, the word and the heads to show.

What this section says

The appendix shows attention weights from the encoder's fifth of six layers on English sentences. Figure 3 follows the word “making”: many heads look far ahead to the words that complete the phrase it begins. Figure 4 shows two heads that appear to resolve anaphora, linking “its” to the noun it refers to, with very sharp weights. Figure 5 shows two heads whose patterns follow the structure of the sentence, each differently.

These pictures back section 4's side benefit, that self-attention may make models easier to interpret. The paper's wording is careful: the heads seem to, appear to. A heat map shows where a head looks, not why the model decides what it does.

The figure above draws the same kind of picture for our model. Pick “it” in layer 2: one head puts nearly all its weight on “dog”, the noun “it” stands for. The German needs that link: “it” becomes “ihn”, “sie” or “es” depending on the noun's gender.

In the paper's words

“Two attention heads, also in layer 5 of 6, apparently involved in anaphora resolution.”

Appendix, Figure 4 In plain words: Two heads of the fifth layer seem to work out what a pronoun refers to.

“The heads clearly learned to perform different tasks.”

Appendix, Figure 5 In plain words: Different heads learned different jobs.

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
\(\text{layer}\) which encoder layer the weights come from 5 of 6 1 or 2 of 2
\(\text{head}\) one of the attentions run side by side, one colour each 8 per layer 4 per layer
\(\text{anaphora}\) a word, such as a pronoun, that refers back to another “its”, in Figure 4 “it”, in our sentences

Try it

How our miniature differs

Our sentences are short and generated by a grammar, so our heads' patterns are clearer than a real corpus would give. On “Anna has a small dog . She likes it .”, head 3 of encoder layer 2 puts 87.9 % of the weight of “it” on “dog”.

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.

Find the pronoun head

In encoder layer 2, which head links “it” most strongly to “dog” in the figure's sentence?

Anaphora

What do the two heads of the paper's Figure 4 appear to do?

Which layer

From which encoder layer of the paper's six are the appendix's pictures taken?

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.