Attention Is All You Need · Section 3.2.1

3.2.1 Scaled dot-product attention

The paper's own attention in one line, softmax(QKᵀ/√d_k)V, and the reason for the √d_k that turned out to matter.

Scaled dot-product attention on our model, step by step
Sentence
The word that asks (the query)
Layer and head
Playback speed

One word of your sentence asks every word: its query is dotted with each key, the scores are divided by √d_k, the softmax turns them into weights, and the values are mixed by those weights. The numbers are our model's, head by head.

What this section says

Scaled dot-product attention is the left half of Figure 2 as one formula. For each query, take its dot product with every key, divide by √d_k, apply the softmax to get weights, and mix the values with them. Written for all queries at once, as matrices, it is equation (1) below.

\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\!\left(\frac{QK^{T}}{\sqrt{d_k}}\right)V \qquad (1)

The paper compares two older ways to score a query against a key. Additive attention feeds both into a small neural network; dot-product attention multiplies them. They cost about the same on paper, but dot products are plain matrix products, which GPUs run very fast. The Transformer's attention is the dot-product kind with one change: the division by √d_k.

Why divide? Earlier work had found that without scaling, additive attention did better for long keys. The paper's explanation: with long keys the dot products become large, the softmax then puts almost all weight on one word, and its gradients, the signal training learns from, become tiny. Footnote 4 makes it precise: if each entry of q and k is a random number with mean 0 and variance 1, their dot product has mean 0 and variance d_k, so its typical size grows as √d_k. Dividing by √d_k brings it back to about 1.

Why this is true: Footnote 4, worked through

The dot product q · k adds up d_k products q_i k_i. Each product has mean 0 (q_i and k_i are independent with mean 0) and variance 1 × 1 = 1.

Variances of independent terms add up, so the sum of d_k such products has variance d_k and standard deviation √d_k. Dividing by √d_k leaves variance 1 whatever d_k is.

\operatorname{Var}(q\cdot k) = \sum_{i=1}^{d_k} \operatorname{Var}(q_i k_i) = d_k, \qquad \operatorname{Var}\!\left(\frac{q\cdot k}{\sqrt{d_k}}\right) = 1

In the paper's words

“While the two are similar in theoretical complexity, dot-product attention is much faster and more space-efficient in practice, since it can be implemented using highly optimized matrix multiplication code.”

Section 3.2.1 In plain words: Additive and dot-product attention cost about the same on paper, but dot products run much faster because they are plain matrix products.

“We suspect that for large values of \(d_{k}\), the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients”

Section 3.2.1 In plain words: With long keys, the dot products become large, the softmax puts almost all its weight on one word, and training receives almost no signal.

“To illustrate why the dot products get large, assume that the components of \(q\) and \(k\) are independent random variables with mean \(0\) and variance \(1\).”

Section 3.2.1 (footnote 4) In plain words: Footnote 4 explains the growth by imagining each entry of a query and a key as a random number with average 0 and spread 1.

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
\(d_k\) numbers in each query and key 64 16
\(d_v\) numbers in each value 64 16
\(\sqrt{d_k}\) what every score is divided by 8 4
\(QK^{T}\) every query dotted with every key: one score per pair of words n × n per head n × n per head
\(\mathrm{softmax}\) turns each row of scores into weights from 0 to 1 that add up to 1 per row per row

Try it

Why divide by √d_k: the experiment
The softmax's largest weight over eight keys, dimension 512, unscaled and scaled A histogram of the largest weight per trial: on average 96.0 % unscaled and 36.3 % scaled. 0 500 1000 1500 0 % 20 % 40 % 60 % 80 % 100 % largest weight trials
  • unscaled q · k
  • q · k / √d_k

Spread of q · k: 22.79 (√d_k = 22.63). Divided by √d_k: 1.01.

One key takes more than 90 % of the weight in 87.1 % of trials unscaled, in 0.0 % scaled.

Random queries and keys with entries of mean 0 and variance 1, for growing d_k. The spread of their dot products grows as √d_k, and the softmax over ten of them collapses onto one word. Divided by √d_k, the spread stays near 1 and the weights stay spread out.

How our miniature differs

Our heads use d_k = 16, so the scores are divided by 4; the paper's d_k = 64 divides by 8. The formula is the same.

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 paper's divisor

The paper uses d_k = 64. What is every score divided by?

Your answer
A whole number

Footnote 4

If the entries of q and k have mean 0 and variance 1, and d_k = 16, what is the variance of q · k?

Your answer
A whole number

Why scale

Why does the paper divide the dot products by √d_k?

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.