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.
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.
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.
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.”
“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”
“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\).”
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 |
|---|---|---|---|
| \(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
- 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.
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 paper's divisor
The paper uses d_k = 64. What is every score divided by?
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?
Why scale
Why does the paper divide the dot products by √d_k?