Answers · How models work
Why does attention use softmax instead of just dividing each score by the sum of all the scores?
In shortDividing by the sum can give negative weights and fails when scores cancel; exponentiating first makes weights positive and turns score gaps into ratios.
Dividing each score by the sum of all the scores normalises magnitude but does not make attention weights safe. Scores are dot products and can be negative, so that division can produce a negative weight or divide by zero; softmax exponentiates first, which forces every weight positive and turns score gaps into fixed ratios.
Those three weights do sum to one: . But the third weight is negative, so the third token's value would be subtracted rather than attended to. The failure is not a rare edge case. If the scores are , the sum is , and dividing by the sum is undefined before any weight exists.
All three softmax weights are positive and their sum is . The first weight divided by the second is , which matches . Dividing the original scores gives for that same pair. The exponential does not merely rescale all scores by a common factor; it fixes the exchange rate between a score gap and a weight ratio. A gap of one gives a ratio of whether the scores are or . Linear division has no such fixed exchange rate: , while , for the same one-point gap.
| Candidate | Non-negative | Sums to one | Differentiable | Order preserving |
|---|---|---|---|---|
| Divide by the sum | no | yes | yes | yes |
| Hard argmax | yes | yes | no | yes |
| Square then normalise | yes | yes | yes | no |
| Softmax | yes | yes | yes | yes |
Why the obvious fix fails
The natural guess is that any normalisation making the weights sum to one would behave the same. The table shows why it does not. Dividing by the sum fails non-negativity as soon as a score is negative. Hard argmax is non-negative and sums to one but has zero gradient almost everywhere, so no learning signal reaches the query and key projections. Squaring removes the sign problem but maps and to the same weight, destroying the ordering the scores were computed to express. Among the obvious candidates softmax is the one that satisfies all four requirements at once. More strongly, any other function satisfying all four is softmax with the score scale changed, so the shape is pinned down and only the exchange rate between score gaps and weight ratios remains free.
Shift invariance and masking
Adding a constant to every score leaves softmax unchanged, because the factor appears in both the numerator and the denominator and cancels. That is not cosmetic. A causal mask needs forbidden positions to receive exactly zero weight. Set their scores to before the softmax: , those positions drop out of the denominator, and the remaining weights renormalise by themselves. With scores the exponentials are , and , the total is , and the weights are and , with the forbidden position at zero. Dividing by the sum cannot express this: is , so the normalisation is undefined rather than exactly zero.
Where it has no answer
Softmax always returns a full distribution. If no token is relevant, the head still has to put its entire weight somewhere, because the weights sum to one and there is no abstention option. Trained models work around this by learning an attention sink, usually a large share on the first token, whose value contributes little. That behaviour is a consequence of the normalisation, not an extra rule. If all scores were guaranteed positive, divide-by-sum would at least satisfy non-negativity, but it would still lack shift invariance and its ratios would depend on the absolute score level. Softmax is not unique as a formula: changing the base or multiplying every score by a constant changes the exchange rate while preserving the four properties. The four properties fix the shape; the scale remains a modelling choice.
Worth keeping
- Dividing scores by their sum only normalises magnitude; it does not force attention weights to be positive.
- Softmax turns a score gap into a fixed weight ratio, so two points of difference always means the same multiplicative advantage.
- Setting a score to negative infinity before softmax gives that position exactly zero weight and renormalises the rest without extra code.
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 Softmax Over Scores, 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 do we divide the attention scores by the square root of the key dimension?The 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.
- 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.