softmax(Q·Kᵀ/√dₖ) converts raw similarity scores into
attention weights (a probability distribution over which tokens to attend
to). Second, at the output: softmax(logits) converts the
final vector into a probability distribution over the vocabulary. Softmax
is the mathematical function that takes the outputs of linear algebra (dot
products, matrix multiplications) and turns them into the language of
probability. It is the bridge.
Step by step:
The result is a valid probability distribution — every entry is non-negative and they sum to exactly 1.
| Property | Why It Matters | Connected Lessons |
|---|---|---|
| Always positive | Probabilities can't be negative (Axiom 1) | 0007 |
| Sums to 1 | Probabilities must sum to 1 (Axiom 2) | 0007 |
| Preserves order | If zᵢ > zⱼ then softmax(z)ᵢ > softmax(z)ⱼ — bigger inputs get bigger probabilities | 0003 |
| Differentiable | We can compute gradients through it for backpropagation | 0002 |
| Amplifies differences | exp() makes big numbers much bigger, separating signal from noise | 0010 |
With a temperature parameter T:
This is the "temperature" slider you see in ChatGPT and other LLM interfaces. Lower temperature = more focused, predictable outputs. Higher temperature = more creative, diverse outputs. You're literally adjusting how peaked the probability distribution is.
In the attention formula, softmax gets Q·Kᵀ/√dₖ — the dot
products are divided by √dₖ before softmax. Why?
As dₖ (the dimension of key/query vectors) grows, dot products grow in magnitude. Large magnitudes pushed through softmax produce near-one-hot distributions — the model becomes overconfident, and gradients vanish (the softmax saturates). Dividing by √dₖ keeps the variance of the dot products roughly constant regardless of dimension. This is a subtle but critical detail that makes deep transformers trainable.
Softmax appears at two critical junctions:
Both are the same function, applied at different scales. The first operates over context positions (1024–128K). The second operates over vocabulary tokens (~50K). Same math, different purpose.
Q1: Input vector z = [1, 2, 3]. After softmax, which entry gets the most probability?
A. The first entry (z=1) B. The third entry (z=3) — but the distribution won't be [0,0,1] C. All entries get equal probability D. It's impossible to determineQ2: If you increase the temperature T in softmax(z/T), what happens to the output distribution?
A. It becomes more peaked (higher confidence) B. It becomes more uniform (flatter, more exploratory) C. The probabilities sum to more than 1 D. Nothing — temperature doesn't affect softmaxQ3: Why is the division by √dₖ in attention softmax(Q·Kᵀ/√dₖ) necessary as dₖ grows?
A. It's an arbitrary convention from the original paper B. Dot products grow with dimension; without scaling, softmax saturates to near-one-hot, killing gradients during training C. It makes the math notation look more impressive D. It's needed to make the output sum to 1