Attention Is All You Need · Section 3.2.2

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.

The heads of one layer, side by side
Sentence
Encoder layer
The word that asks (the query)
Layer 2, head 1 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna90 %has40 %23 %a53 %44 %small52 %39 %dog96 %.14 %13 %34 %12 %She91 %likes31 %39 %10 %it13 %37 %33 %.15 %16 %30 %13 %</s>12 %22 %17 %15 %10 %

From “it” it looks most at “.” (37.4 %).

Layer 2, head 2 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna42 %42 %has32 %17 %20 %a35 %13 %10 %13 %small54 %15 %dog14 %11 %41 %.17 %28 %She12 %13 %30 %34 %likes24 %27 %30 %it16 %18 %19 %.17 %30 %</s>15 %12 %18 %13 %

From “it” it looks most at “</s>” (18.6 %).

Layer 2, head 3 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna85 %has35 %13 %a99 %small21 %48 %dog15 %15 %19 %13 %15 %.14 %22 %17 %12 %She11 %43 %likes17 %25 %18 %it88 %.16 %16 %15 %12 %</s>13 %14 %10 %15 %10 %21 %

From “it” it looks most at “dog” (87.9 %).

Layer 2, head 4 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna21 %11 %11 %12 %has18 %15 %15 %37 %a91 %small58 %31 %dog32 %35 %17 %.15 %24 %She16 %10 %18 %22 %15 %10 %likes18 %18 %24 %15 %it25 %14 %34 %.17 %24 %</s>37 %13 %11 %13 %

From “it” it looks most at “dog” (34.3 %).

Playback speed

The layer's attention output for “it”, first 8 of 64 numbers
Token12345678… 56 more
All heads on0.450.120.62−0.710.15−0.171.11−0.06…
As switched now0.450.120.62−0.710.15−0.171.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.

\mathrm{MultiHead}(Q,K,V) = \mathrm{Concat}(\mathrm{head}_1,\ldots,\mathrm{head}_h)\,W^{O}, \qquad \mathrm{head}_i = \mathrm{Attention}(QW^{Q}_i,\, KW^{K}_i,\, VW^{V}_i)

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

Section 3.2.2 In plain words: Several heads let the model look at different kinds of information, in different places, at the same time.

“With a single attention head, averaging inhibits this.”

Section 3.2.2 In plain words: One head on its own would average these different kinds of information together.

“Due to the reduced dimension of each head, the total computational cost is similar to that of single-head attention with full dimensionality.”

Section 3.2.2 In plain words: Each head works on a smaller slice of the vector, so all the heads together cost about as much as one big head.

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
\(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

The cost of h heads, counted
Vector size, d_model
Heads, h
A vector of 512 numbers cut into 8 heads of 64 numbers eachd_k = 64d_k = 64d_k = 64d_k = 64d_k = 64d_k = 64d_k = 64d_k = 64d_model = 512
The cost of 8 heads against one head of full size, for d_model = 512 and a sentence of 10 words
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).

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

Your answer
A whole number

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

Your answer
A whole number

The paper's cost claim

How does the cost of h heads of size d_model / h compare with one head of full size?

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.