Name the two properties softmax guarantees for every row of the attention weight matrix
Every entry lies in [0, 1] and each row sums to exactly 1. Softmax produces a row-stochastic matrix, a valid probability distribution over keys.
Picture cutting a single pizza into slices to share among friends. No friend can get a slice of negative size, no friend can get more than the whole pizza, and the total amount of pizza handed out has to add up to exactly one pizza, no more, no less. The step that turns raw model scores into attention works the same way. Each item on the menu gets a fraction of the model's focus that is at least 0 and at most 1, and the fractions across the whole row always sum to exactly one pizza's worth. That tidy 'no negatives, no overweights, total of one' rule is what makes the row behave like a clean share-out.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Softmax guarantees two properties on every row of the attention weight matrix: each entry lies in [0, 1], and the entries sum to exactly 1. Together these make each row a valid probability distribution over the keys, equivalently a point on the probability simplex. The simplex constraint is the algebraic substrate of essentially every important property of attention: the convex-combination interpretation of the output, the way masking works, and the structural inevitability of attention sinks.
This deep dive proves both properties from the softmax formula, walks the probability-simplex interpretation, traces the three main downstream consequences, and covers the edge cases (fully masked rows, softmax-1, hard attention) where the constraints behave differently.
Mental model: softmax is the operator that takes arbitrary real-valued scores and slices them onto the probability simplex. The two properties together are the simplex constraint.
Proving the two properties from the formula
The softmax operator on row i of the score matrix produces:
Non-negativity (w_{ij} >= 0)
The exponential function e^x is strictly positive for every real x. So:
- The numerator
e^{s_{ij}}is positive. - The denominator
sum_k e^{s_{ik}}is a sum of positives, so positive. - The ratio is positive.
The only way to get exactly 0 is to feed in s_{ij} = -inf, which is what masking does deliberately.
Upper bound (w_{ij} <= 1)
The denominator includes the numerator as one of its summands:
denominator = e^{s_{ij}} + sum_{k != j} e^{s_{ik}}
The other summands are all positive, so denominator >= numerator, so w_{ij} = numerator / denominator <= 1. Equality w_{ij} = 1 is approached but not reached in practice; in the limit, one score dominates and all others are infinitely smaller.
Row sum = 1
Sum the row entries:
The numerator and denominator are the same sum (just with different dummy indices), so the ratio is exactly 1.
Key insight: the row-sum-to-1 property is not approximate. It is exact by construction, holding even after numerical operations like the row-max stabilization trick
softmax(s - max(s)).
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Vaswani et al. 2017: the canonical attention formula softmax(QK^T / sqrt(d_k)) V produces row-stochastic weight matrices, the [0, 1] entries and row-sum-to-1 are why the output is a convex combination of values.
- Attention visualizations (BertViz, exBERT) display the post-softmax weight matrix as a heatmap where each row's cells visually add up to 1.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat changes about the [0, 1] and sum-to-1 properties when softmax-1 is used in place of standard softmax?
Softmax-1 computes w_j = exp(s_j) / (1 + sum_k exp(s_k)). The +1 in the denominator means each row entry is still in [0, 1], but the row sum is now in [0, 1) rather than exactly 1. The model can express 'no key matters' by letting the row sum approach 0. This eliminates the structural cause of attention sinks but requires retraining since gradients flow differently.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Forgetting that weights must be NON-NEGATIVE and at most 1. Softmax cannot produce negative weights or weights larger than 1, regardless of the score values.
60 second bullets to scan on the way to the call.
The range [0, 1] for every softmax-normalized attention entry
Why every entry is non-negative (exponential is positive)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.