02 Chapter 2 · softmax
Softmax
Subtract the max, then exp, then divide. The largest exp is 1. A naive exp can overflow while this one stays finite.
softmax (x)ᵢ = exp(xᵢ − max x) / Σⱼ exp(xⱼ − max x)
Four scores. Mild and sharp stay in a range where exp(x) is finite either way. Overflow uses values near 1000, where exp(x) is Inf and exp(x − max) is not. The weights of the stable path still sum to 1. Same ratios, no overflow — the subtraction is the whole trick.
softmax · max, subtract, exp, normalize
Raw scores. Play to mark the max.
Arrow keys step, space plays or pauses.
This browser prefers reduced motion. Play still runs the instruction and jumps straight to the result.
Reading the picture
-
The max is the whole trick
Subtracting the max shifts the peak to 0. exp(0) is 1, and every other exp is smaller. The ratios do not change, so the weights do not change.
-
Naive exp overflows
exp(1000) is Inf. The overflow preset shows those values going Inf, while the shifted ones stay between −3 and 0 and the weights still sum to 1.
-
Where it sits in attention
Attention is QKᵀ, this softmax, then a mix with V. The GEMM pages never do this step. Open the attention page
Mild scores: 1.20, 0.30, 2.00, 0.70.