Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Turning scores into probabilities
A classifier’s last layer produces one score per class. Softmax exponentiates each score, which makes it positive and exaggerates differences, then divides by the total so the results sum to 1. Adding the same number to every score changes nothing, which is the key to computing it safely. Log-softmax returns the logarithms of those probabilities directly, without ever forming tiny numbers.
| naive softmax exp(z)/Σexp(z) | 0.839, 0.114, 0.006, 0.042 |
| stable softmax (subtract max) | 0.839, 0.114, 0.006, 0.042 |
| loss as −log(softmax)[true] | 5.176 |
| loss as −log-softmax[true] | 5.176 |
| gradient p − y | 0.839, 0.114, -0.994, 0.042 |
Every perception classifier (terrain type, object class, fault mode) ends in softmax, and every transformer uses it inside attention. On GPUs and accelerators in float16, bfloat16 or float32, a naive implementation overflows or underflows far sooner than in float64, producing NaN weights or infinite losses mid-training, often after hours. Fused, stable implementations are what prevent this.
- 1. Softmax outputs…
- 2. Adding 100 to every logit changes softmax…
- 3. Log-softmax is useful because…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Properties by hand
- Definition: pᵢ = exp(zᵢ)/Σⱼ exp(zⱼ). For z = (0, 0): (0.5, 0.5). For z = (0, log 3): (0.25, 0.75).
- Shift invariance: softmax(z + c) = softmax(z), so compute it as exp(z − max z)/Σ exp(z − max z), where the largest exponent is 0.
- Log-sum-exp: LSE(z) = log Σ exp(zⱼ) = m + log Σ exp(zⱼ − m), with m = max z. LSE(1000, 1000) = 1000 + log 2.
- Log-softmax: log pᵢ = zᵢ − LSE(z).
- Temperature: softmax(z/T). T → 0 approaches a one-hot vector at the argmax; T → ∞ approaches uniform. The entropy increases with T.
- Jacobian: ∂pᵢ/∂zⱼ = pᵢ(δᵢⱼ − pⱼ), that is, diag(p) − ppᵀ. Its columns sum to 0. For p = (0.5, 0.5) it is [[0.25, −0.25], [−0.25, 0.25]].
- Cross-entropy gradient: L = −log p_y gives ∂L/∂z = p − y (one-hot y). For p = (0.7, 0.2, 0.1) and true class 1: (0.7, −0.8, 0.1).
- Two classes: softmax over (z, 0) is (σ(z), 1 − σ(z)): the logistic sigmoid.
- 1. The cross-entropy gradient with respect to the logits is…
- 2. As T → 0, softmax(z/T) approaches…
- 3. Softmax over two logits (z, 0) gives the first class…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Implementing it safely
Exercise 1.
- Softmax is shift-invariant.
- At a logit gap of 1000, log(softmax) gives −∞ while log-softmax returns the exact −1000.31.
- The cross-entropy gradient equals (p − y)/n by finite differences, and the Jacobian is diag(p) − ppᵀ.
- Temperature moves softmax from one-hot to uniform.
- float32 limits: exp overflows above 88.72, and a probability underflows to exactly 0 when its logit is about 103.97 below the largest. In float16 the overflow threshold is about 11.1.
Rules.
- Never compute exp(z) without subtracting the maximum.
- Never compute log(softmax(z)); compute log-softmax.
- Pass logits, not probabilities, to the loss.
F.cross_entropy(logits, y)fuses log-softmax and the negative log-likelihood, so its gradient is exactly p − y. - In attention, the same rules apply, together with masking by −∞ (or a large negative number) before the softmax.
F.cross_entropy(logits, y) # stable: fused log-softmax + NLL
F.nll_loss(torch.log(F.softmax(z, -1)), y) # unsafe: log of probabilities that can be 0- 1. float32 exp overflows above about…
- 2. Which call is numerically safe?
- 3. The softmax Jacobian's columns sum to…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why the gradient is so simple. L = −z_y + LSE(z), and ∂LSE/∂zⱼ = pⱼ, so ∂L/∂z = p − y. The exponentials in softmax and the log in cross-entropy cancel. The result is bounded in [−1, 1] per component, which is why softmax cross-entropy trains so reliably.
Softmax as a smooth argmax. LSE(z) is a smooth maximum: max z ≤ LSE(z) ≤ max z + log K. Softmax is its gradient. With temperature, T·LSE(z/T) → max z as T → 0.
Probabilistic meaning.
- Softmax is the Gibbs (Boltzmann) distribution with energies −zᵢ.
- It is the maximum-entropy distribution with given expected scores.
- It is the link function of multinomial logistic regression.
Overconfidence and saturation. Cross-entropy keeps pushing logits apart on separable data, since the loss only reaches 0 at infinite margin. Logit norms grow, which is what drives logits into the overflow range in long float32 or float16 training. Remedies:
- label smoothing;
- weight decay;
- logit normalisation;
- temperature scaling after training, for calibration (Guo et al., 2017).
Gradient through the unsafe path. Automatic differentiation of log(softmax(z)) computes ∂L/∂p_y = −1/p_y and multiplies by ∂p_y/∂z = p_y(e_y − p). When p_y underflows to 0, this is −∞ × 0 = NaN, even though the true gradient is finite.
- 1. ∂LSE(z)/∂zⱼ =
- 2. The bound on LSE(z) − max z is…
- 3. Autodiff through log(softmax) produces NaN because…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Which implementations break, and how?
History.
- The Boltzmann distribution (L. Boltzmann, 1868; J. W. Gibbs, 1902) is softmax with energies.
- R. Luce (1959) used it as a choice model.
- J. Bridle (1990) named “softmax” and introduced it for neural network outputs.
- Attention (D. Bahdanau et al., 2015; A. Vaswani et al., 2017) made softmax the core of transformers.
Exercise 2. Multinomial logistic regression on three well-separated classes, trained by gradient descent in float32 at learning rate 1 for 400 steps. Multiplying the inputs by 10 or 100 makes the logits large. Four implementations of the same loss and gradient are compared:
- naive softmax;
- stable softmax, then log, with the gradient by the chain rule (as autodiff would);
- the same, with the fused gradient p − y;
- log-softmax via log-sum-exp.
| implementation | input scale | steps with non-finite loss (of 400) | weights became NaN at step | final accuracy |
|---|---|---|---|---|
| naive softmax | 1 | 0 | never | 0.990 |
| naive softmax | 10 | 399 | 2 | — |
| naive softmax | 100 | 399 | 2 | — |
| softmax, then log, chain rule | 10 | 399 | 2 | — |
| softmax, then log, chain rule | 100 | 399 | 2 | — |
| softmax, then log, fused gradient | 10 | 165 | never | 0.987 |
| softmax, then log, fused gradient | 100 | 399 | never | 0.987 |
| log-softmax | 10 | 0 | never | 0.990 |
| log-softmax | 100 | 0 | never | 0.987 |
At input scale 1, all four implementations behaved identically.
- The naive softmax failed at once. After one large step the logits passed 88.72, exp overflowed, ∞/∞ gave NaN probabilities, and the weights became NaN.
- Shifting alone was not enough when autodiff goes through the log. Misclassified points with logit gaps above about 104 have p_y = 0 exactly, the chain rule gives −∞ × 0 = NaN, and the weights broke at the same step.
- The fused gradient trained correctly but reported an infinite loss in 165 or 399 of 400 steps. Any loss-based logic (early stopping, learning-rate schedulers, logging) would have been broken.
- Log-softmax never failed. Only the fused, log-sum-exp implementation was correct in both loss and gradient.
Softmax in AI, robotics and aerospace
- Perception classifiers for terrain, obstacles and targets use softmax outputs, often calibrated by temperature scaling before being fused with other sensors.
- Attention in transformers for trajectory prediction and multimodal perception is a softmax over similarity scores. Its numerics in float16 or bfloat16 are a common failure point (FlashAttention computes it blockwise with a running max).
- Decision making: softmax (Boltzmann) policies in reinforcement learning choose actions with probability proportional to exp(Q/T).
References
- J. S. Bridle (1990). “Probabilistic interpretation of feedforward classification network outputs, with relationships to statistical pattern recognition.” In Neurocomputing, Springer.
- C. Guo, G. Pleiss, Y. Sun, K. Q. Weinberger (2017). “On calibration of modern neural networks.” Proc. ICML.
- P. Blanchard, D. J. Higham, N. J. Higham (2021). “Accurately computing the log-sum-exp and softmax functions.” IMA Journal of Numerical Analysis 41(4).
- T. Dao, D. Fu, S. Ermon, A. Rudra, C. Ré (2022). “FlashAttention: fast and memory-efficient exact attention with IO-awareness.” Advances in NeurIPS 35.