Core
Scaled dot-product attention & the √d_k
Dividing query–key scores by the square root of their dimension keeps softmax usable as attention width grows.
Updated
1
Concept
Self-attention begins with a simple compatibility measure: the dot product between a query and a key. If two vectors point in similar directions, their dot product tends to be positive and large. If they are poorly aligned, it is smaller or negative. For a query matrix and key matrix , all pairwise scores appear in . But the raw score scale changes when the vector dimension changes.
Write one score as
For an intuition used in the original Transformer paper, suppose components are independent, have mean zero, and variance one. Each product then has variance near one. Adding such terms gives , so the score’s standard deviation grows like . A 256-dimensional head therefore produces wider raw score variation than a 16-dimensional head even when the component statistics are otherwise comparable.
Attention sends each row of scores through softmax. Softmax exponentiates its inputs, so scale matters dramatically. Scores produce a fairly distributed result. Scores produce an almost winner-take-all result. The ranking is identical, yet the second distribution behaves as if the model were extremely certain. When probabilities approach zero or one, many useful gradients become small, which can make learning brittle.
Scaled dot-product attention corrects this dimension effect:
Dividing by approximately normalizes the score standard deviation under the assumptions above. It does not force exact variance one in a trained network, and it does not normalize each query or key vector. Learned projections, normalization layers, correlations, and real activations violate the tidy independence story. The scaling is still a robust design choice because it removes the most obvious growth with dimension.
The divisor can also be viewed as a fixed temperature. Softmax temperature is usually written . Here . A larger temperature flattens a distribution; a smaller one sharpens it. In attention the scale is tied to head dimension rather than exposed as a decoding control. A model can still learn sharper or flatter patterns by changing query and key norms.
Numerical stability adds another layer. Practical softmax implementations subtract the row maximum before exponentiating:
Subtracting a constant does not change the ratio of probabilities, but it prevents a large positive score from overflowing the exponential. This technique solves numerical overflow; it does not solve saturation caused by score gaps. The scaling addresses the latter source at initialization and during early training.
Consider scores from one query against three keys. If the raw dot products are and , scaling produces . Softmax can assign meaningful mass to all three, allowing gradients to adjust each relationship. Without scaling, the first key would dominate much more strongly before the model had evidence that it deserved such certainty.
The lesson is broader than memorizing a denominator. Whenever a sum collects many roughly independent contributions, its typical magnitude often grows with the number of terms. Passing that sum into a sensitive nonlinearity can change behavior merely because width changed. The Transformer compensates with a scale derived from variance, keeping the routing mechanism in a trainable regime.
2
Explain it like I am five
Suppose judges score a performance by adding marks from many independent categories. A show evaluated on 64 categories naturally accumulates larger positive and negative totals than one evaluated on four, even if quality per category is unchanged. Sending those raw totals into a winner-take-all rule would make the larger scorecard look absurdly certain. Dividing by the square root of the category count restores a comparable scale before the final vote.
3
Teach it back
Derive why dot-product variance grows with key dimension and explain what dividing by square root of d_k does to softmax.
Minimum: 80 characters and 15 words. Your text stays only in this browser.
Saved only on this device.
Show a model answer
If query and key components are roughly independent with zero mean and unit variance, each product contributes variance near one. Summing d_k products gives variance near d_k, so the standard deviation grows as square root of d_k. Dividing scores by that standard deviation keeps their typical magnitude roughly stable, preventing softmax from saturating solely because the head dimension increased.
4
Check your understanding
Complete the teach-back and answer the quiz correctly to finish this lesson.
Sources
- Ashish Vaswani et al. (2017). Attention Is All You Need.