Answers · How models work
Why do we divide the attention scores by the square root of the key dimension?
In shortThe square root of the key dimension is the typical size of an unscaled attention score, so dividing by it stops softmax from becoming a hard pick-one.
The square root of the key dimension is the standard deviation of an unscaled attention score, so dividing by it holds the spread of the scores at one however wide the head happens to be. It is a derived number, not a tuning knob: it falls out of the variance of a dot product between independent vectors. Any other constant would fail at some head width, and the failure is not cosmetic, because softmax converts a change of scale into a change of sharpness.
Where the spread comes from
Fix one head with key dimension , and suppose the components of a query and a key are independent random variables with mean zero and variance one. That is roughly what a fresh initialisation gives, and roughly what normalisation maintains. A single attention score is their dot product, a sum of products:
Each term is a product of two independent mean-zero variables, so its mean is and the whole score is centred. For the variance of one term, independence lets the expectation of the product of squares factorise, so . Every term therefore contributes exactly one unit of variance. The terms are independent of one another, and independent variances add, so of them give a variance of .
- the dimension of the query and key vectors inside one head
- variance, the average squared distance from the mean
- standard deviation, the square root of the variance, measured in score units
Read the numbers off the formula. With a typical score sits about from zero, at the of the original transformer about , and at about . Dividing each score by brings all three of those back to . That is the whole content of the denominator: it restores a fixed unit of spread to a quantity whose spread was drifting with the width of the head.
What a score of 8 does to softmax
Softmax sees only differences between scores, and it turns a difference into a ratio of weights of . Multiply every score by a constant and every difference becomes , so every ratio is raised to the power : the distribution sharpens. This is exactly a temperature. With raw scores of spread , take five of them, , , , , , and exponentiate: , , , , , summing to . The top weight is . The second token is only four raw points behind, and gets , a factor of smaller.
| step | raw score | weight unscaled | scaled score | weight scaled | what happened |
|---|---|---|---|---|---|
| 1 | 12 | 0.98 | 1.5 | 0.43 | Top token. Unscaled it takes 98 per cent of the attention. |
| 2 | 8 | 0.018 | 1 | 0.26 | Four raw points behind, yet 55 times smaller in weight. |
| 3 | 4 | 0.0003 | 0.5 | 0.16 | Still a plausible match, effectively erased. |
| 4 | 0 | 6e-06 | 0 | 0.1 | Neutral. |
| 5 | -4 | 1e-07 | -0.5 | 0.06 | A poor match, downweighted in both columns. |
Divide by instead and the scores become , , , , , with exponentials , , , , summing to . The weights are , , , , . The top two are now apart, which is , exactly as it must be, since the gap shrank from to . The information in the second and third tokens survives.
Why a smaller number is not enough
The natural guess is that the division is a general trick for keeping numbers small, so any constant that brings the magnitudes down would serve, or that was found by experiment. Suppose you fixed the divisor at regardless of width. At the raw spread is , so a gap of one standard deviation becomes after dividing, and the weight ratio is . At the raw spread is , the same standard-deviation gap becomes , and the ratio is . The head is saturated at one width and washed out at another, and the constant that was correct at is wrong at both. Dividing by gives a gap of exactly in both cases and a ratio of . The scale factor has to track the width because the standard deviation does; no single number does that.
The damage from getting it wrong is not confined to ugly weights. The derivative of softmax output with respect to input is a product of probabilities, so it is largest when the weights are moderate and collapses when they are extreme. On the diagonal the derivative is : at the scaled top weight of that is , while at the unscaled it is . Off the diagonal it is , so the gradient reaching the third token's score is when scaled against when not, roughly two hundred times smaller. Almost no signal reaches the projections, and the head cannot learn to attend differently because the confidence has switched off the mechanism by which it would learn.
- off the diagonal this is minus the product of two probabilities, tiny unless both are moderate
- on the diagonal, largest at a_i = 1/2 and tending to zero at both 0 and 1
What the derivation assumes
The argument needed two things: that query and key components are roughly independent, and that they have roughly unit variance. Neither is exactly true in a trained network. Learned projections introduce correlations between components, and the actual variance depends on where the block sits and how it is normalised. If the components have variance rather than , the score variance becomes and the correct divisor is , so a fixed under-corrects as the variance drifts upward with depth. That is why the placement of layer normalisation changes how stable training is, and why variants that normalise queries and keys directly before the dot product exist at all: both are repairs to the assumption rather than to the algebra. The constant is worth knowing precisely because its assumptions are visible; when they fail, the constant is the part you adjust.
Dividing by leaves the score distribution with unit spread whatever the head width, which keeps softmax graded, which keeps gradients alive. A wide head without it does not attend badly so much as commit to an answer before it has learned anything.
Worth keeping
- The variance of an unscaled attention score is the key dimension, so its standard deviation is the square root of that dimension.
- Softmax is exponential in score gaps, so multiplying all scores by a constant raises every weight ratio to that constant and changes sharpness without changing order.
- When one softmax weight approaches 1, every partial derivative is a product of probabilities and collapses, so an unscaled head stops receiving gradient.
Where this goes next
This question is one step of The Mathematics of Attention, a course that works through its subject from the beginning. The page that teaches this part of it is The Scaling Factor, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What do multiple attention heads actually give you that one big head doesn't?Several heads let a model track several relationships at once, each in its own lower-dimensional subspace, instead of squeezing every relationship into one weighted average that destroys information.
- Why does attention use softmax instead of just dividing each score by the sum of all the scores?Dividing by the sum can give negative weights and fails when scores cancel; exponentiating first makes weights positive and turns score gaps into ratios.
- How does masking actually make attention causal?The mask is applied to the scores before they become weights, so forbidden positions end up with exactly zero weight and the rest are renormalised automatically.
- Why does attention need positional encoding if it already looks at the whole sequence at once?Attention treats tokens as an unordered set, so position has to be added to the vectors; otherwise a sentence and its anagram get the same representations.
- Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
- Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.