What does the softmax produce in the attention equation?
Last updated
The softmax turns raw query-key similarity scores into attention weights. For each query you get a probability distribution over the keys: non-negative weights that sum to 1. These weights then combine the values.
Shape and meaning:
Input: the logits
QKᵀ/√d_k, with shapen_q × n_k.Output: a matrix of the same shape where each row sums to 1.
Entry
(i, j)is how much queryireads from tokenj.
Why it matters:
Multiplying the weights by
Vgives a convex combination of values, so the output stays on a stable scale whatever the sequence length.The exponential amplifies differences between scores, so attention can be sharp (close to selecting one token) or diffuse.
It is differentiable, unlike a hard argmax lookup.
Masking hooks in here:
Masked positions are set to
-infbefore the softmax, so they get exactly 0 weight.This is how causal masks and padding masks work.
Caveat: the weights must sum to 1, so a head cannot attend to nothing. This produces "attention sinks" (heavy weight on the first token), which some variants address with an extra null slot or softmax-off-by-one.