What does the softmax produce in the attention equation?

Last updated 22 November 2026

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 shape n_q × n_k.

    • Output: a matrix of the same shape where each row sums to 1.

    • Entry (i, j) is how much query i reads from token j.

  • Why it matters:

    • Multiplying the weights by V gives 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 -inf before 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.

  • https://www.geeksforgeeks.org/nlp/transformer-attention-mechanism-in-nlp/
  • https://github.com/purepisces/Wenqing-Machine_Learning_Blog/blob/main/Machine-Learning-Interview-Questions/Transformer-Interview-Question.md
  • https://neuraprep.com/blog/deep-learning-transformers-interview-questions/
All Transformers interview questions