3.2.2 Multi-head attention
Instead of one attention over the whole vector, the paper runs h smaller ones side by side, each on its own projection, and joins what they find.
From “it” it looks most at “.” (37.4 %).
From “it” it looks most at “</s>” (18.6 %).
From “it” it looks most at “dog” (87.9 %).
From “it” it looks most at “dog” (34.3 %).
| Token | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … 56 more |
|---|---|---|---|---|---|---|---|---|---|
| All heads on | 0.45 | 0.12 | 0.62 | −0.71 | 0.15 | −0.17 | 1.11 | −0.06 | … |
| As switched now | 0.45 | 0.12 | 0.62 | −0.71 | 0.15 | −0.17 | 1.11 | −0.06 | … |
Every head is on: switch one off to see how much the output changes.
The heads of one attention layer of our model as heat maps on your sentence, one row per word that asks and one column per word it looks at. Each head has learned a different pattern. Switch a head off and the layer's output, recomputed without it, changes; then the heads' outputs are joined and multiplied by W^O.
What this section says
A single attention over vectors of d_model numbers gives each word one set of weights, one way of looking at the sentence. Multi-head attention instead projects the queries, keys and values h times, with different learned matrices, to vectors of only d_k and d_v numbers, and runs scaled dot-product attention on each projection in parallel. Each head can learn a different relation: one the previous word, another the noun a pronoun stands for.
The h outputs, each of d_v numbers, are joined end to end (Concat) and multiplied by one more matrix, the output matrix W^O, which mixes them back into d_model numbers. The paper uses h = 8 heads with d_k = d_v = 512 / 8 = 64.
Why this costs little extra: each head is h times smaller, so h of them do about the work of one head of full size. The counter below shows it: change h and the total number of weights and multiplications stays put.
In the paper's words
“Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.”
“With a single attention head, averaging inhibits this.”
“Due to the reduced dimension of each head, the total computational cost is similar to that of single-head attention with full dimensionality.”
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 |
|---|---|---|---|
| \(h\) | heads, run side by side | 8 | 4 |
| \(d_k = d_v = d_{\text{model}}/h\) | numbers per query, key and value in one head | 64 | 16 |
| \(W^{Q}_i,\ W^{K}_i,\ W^{V}_i\) | head i's own projection matrices, d_model × d_k each | 512 × 64 | 64 × 16 |
| \(W^{O}\) | the output matrix that mixes the joined heads, h·d_v × d_model | 512 × 512 | 64 × 64 |
Try it
| Count | 8 heads | One head of full size |
|---|---|---|
| Numbers per query, key and value (d_k) | 64 | 512 |
| Weights of W^Q, W^K, W^V and W^O | 1,048,576 | 1,048,576 |
| Multiplications for one word's scores | 5,120 | 5,120 |
Pick a number of heads for the paper's d_model = 512 or our 64. Each head's slice of the vector shrinks as h grows, so the weights of all the projections and W^O, and the multiplications for one word's scores, stay the same.
How our miniature differs
Our model has 4 heads of d_k = 16 per attention layer, 6 attention layers in all (self-attention in each encoder layer, two attentions in each decoder layer). Our Table 3 reruns tried 1 and 16 heads (section 6.2).
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.
The size of a head
With d_model = 512 and h = 8, how many numbers does each head's query have (d_k)?
Weights do not depend on h
With d_model = 64, how many weights do all heads' W^Q, W^K, W^V and W^O hold together (no biases)?
The paper's cost claim
How does the cost of h heads of size d_model / h compare with one head of full size?