Attention Is All You Need · Section 3.2

3.2 Attention

What an attention function is, in two sentences, and the paper's Figure 2: the attention of one head on the left, many heads side by side on the right.

Figure 2, redrawn: both halves
The paper's Figure 2, redrawn: scaled dot-product attention and multi-head attention Scaled Dot-Product AttentionMulti-Head Attention h Q K V MatMul Scale Mask (opt.) SoftMax MatMul V K Q Linear Scaled Dot-Product Attention Concat Linear

Scale · Figure 2, left: scaled dot-product attention

Every score divided by √d_k, so long keys do not make the scores so large that the softmax picks a single word.

What it produces, for a sentence of n tokens: n × n in the paper, n × n in our model.

The paper's Figure 2 redrawn. Left, scaled dot-product attention from the bottom up: Q and K multiplied (MatMul), scaled, optionally masked, turned into weights by the softmax, and multiplied with V. Right, multi-head attention: V, K and Q each projected by h learned Linear layers, h scaled dot-product attentions side by side, their outputs joined (Concat) and mixed by a last Linear layer. Click any box to see what it does and the shape of what it produces, in the paper and in our model.

What this section says

Section 3.2 defines attention in general before the paper's own kind. An attention function takes a query and a set of key–value pairs, all of them vectors, and returns a vector. The query is what a word is looking for; each key says what a word offers; each value is what that word passes on.

The output is a weighted sum of the values. Each value's weight comes from comparing the query with that value's key: the better they match, the larger the weight. The paper calls the comparison a compatibility function; its own choice, the scaled dot product, is the left half of Figure 2 (section 3.2.1).

The right half of Figure 2 runs the left half several times side by side, each time on a different learned projection of the same vectors, and joins the results (section 3.2.2). The box Mask (opt.) is used only in the decoder's self-attention (section 3.2.3).

In the paper's words

“An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors.”

Section 3.2 In plain words: Attention takes a query and a list of key and value pairs, all vectors, and returns a vector.

“The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key.”

Section 3.2 In plain words: The result mixes the values, each weighted by how well the query matches that value's key.

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,\ Q\) a query; Q is all queries as the rows of a matrix rows of 64 numbers per head rows of 16 numbers per head
\(k,\ K\) a key; K is all keys as rows rows of 64 numbers per head rows of 16 numbers per head
\(v,\ V\) a value; V is all values as rows rows of 64 numbers per head rows of 16 numbers per head
\(h\) the number of heads run side by side (the stack behind the right half's boxes) 8 4

Try it

Three matrices, three vectors for every word
Playback speed

Charles's example, “She eats apples”, with each word as two numbers. Step by step, each word's vector is multiplied by the three small matrices W_Q, W_K and W_V, and the products fill three tables: the queries Q, the keys K and the values V. The readout writes each multiplication out.

How our miniature differs

Figure 2's boxes are the same in our model; only the sizes differ: 4 heads instead of 8, each with queries, keys and values of 16 numbers instead of 64.

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.

What comes out

What is the output of an attention function?

The optional box

Which box of Figure 2's left half is used only in the decoder's self-attention?

The weights add up

The weights one query gives to all the values: what do they add up to?

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.