Softmax converts a vector of real scores into nonnegative values that sum to one. Directly exponentiating very large scores can overflow in floating-point arithmetic.
Worked example
For scores one thousand and one thousand one, subtract the largest score first. The adjusted values are minus one and zero. Exponentiating those values and normalizing gives the same softmax probabilities in exact arithmetic, because subtracting a common constant multiplies every exponential by the same factor.
What to check
This transformation prevents overflow from large positive inputs, while very small contributions may still underflow toward zero. Use a numerically stable log-softmax or log-sum-exp operation when a loss requires log probabilities. Avoid taking the logarithm of a rounded probability when a stable combined operation is available. Verify that scores are finite and that the intended normalization axis matches the model’s class or token dimension.
Continue with the book
Explore the full treatment, worked examples, and exercises in Mathematics for AI and Machine Learning.
Companion article for Mathematics for AI and Machine Learning, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
