Softmax
Subtracting before exponentiating matters because naive overflows floating-point precision (inf / inf = NaN) even for modest logits (around in float64 or in float32), whereas shifting ensures every exponent satisfies so while leaving the exact mathematical ratios unchanged.
Softmax converts a vector of arbitrary real scores (logits) into positive probabilities that sum to , amplifying larger entries exponentially relative to smaller ones. Subtracting the maximum score from every entry before taking exponentials keeps each exponential bounded at or less, preventing floating-point overflow without changing the final probabilities.
Transformers and classifiers apply softmax both to transform final vocabulary logits into next-token probabilities and to normalize query-key scores into attention weights inside every attention head.
Take raw logits :
- Subtract max for stability: , giving shifted logits .
- Exponentiate: , , and .
- Sum exponentials: .
- Divide by the sum:
CODE RUNS ONLY IN YOUR BROWSER VIA PYODIDE WEBASSEMBLY — NOTHING IS SENT TO ANY SERVER.
QUESTION 1 / 3
0 / 3 ANSWEREDQ1 · MCQ
Why does a numerically stable softmax implementation subtract max(z) from every logit before exponentiating?
NO DOWNSTREAM FORMULAS YET
Nothing downstream lists Softmax as a prerequisite yet. Open all 20 curriculum sections to pick what to prove next.
BROWSE ALL 20 SECTIONS →