Skip to content

Softmax and log-sum-exp

Convert class scores to probabilities, compute log-sum-exp without overflowing, and distinguish harmless score shifts from temperature scaling and calibration.

Softmax converts a finite vector of real scores into positive weights that sum to one. In a classifier, these weights represent a model's probabilities for mutually exclusive classes. Log-sum-exp is the natural logarithm of the sum of the scores' exponentials: the scalar normalising quantity behind softmax. Computing both carefully lets a quant model produce class probabilities and likelihood losses even when the raw exponentials exceed the computer's range.
For K≥2K\ge2 finite real scores, or logits, z1,…,zKz_1,\ldots,z_K, define
L=LSE⁡(z)=log⁡(∑j=1Kezj),pi=softmax⁡(z)i=ezi∑j=1Kezj.L=\operatorname{LSE}(\mathbf{z})=\log\left(\sum_{j=1}^{K}e^{z_j}\right), \qquad p_i=\operatorname{softmax}(\mathbf{z})_i=\frac{e^{z_i}}{\sum_{j=1}^{K}e^{z_j}}.

LSE returns one number; softmax returns a vector. Their connection is also differential: ∂L/∂zi=pi\partial L/\partial z_i=p_i. The largest score receives the largest probability, but softmax does not select a single winner.

The level of the scores is irrelevant

Imagine a fictional classifier assigning each trading day to exactly one of three classes, A, B or C. Its dimensionless logits are 1000, 1001 and 1002. These are scores, not returns or probabilities.

Directly evaluating e1002e^{1002} causes overflow in standard binary64 arithmetic. Yet the desired probabilities are perfectly ordinary. Subtract the largest score, m=1002m=1002, before exponentiating:
ClassOriginal scoreScore minus maximumShifted exponentialProbability
A1000−20.1353350.090031
B1001−10.3678790.244728
C100201.0000000.665241

The shifted exponentials sum to approximately 1.503215. Dividing each by this sum gives probabilities that sum to one, apart from rounding. Subtracting any common constant leaves softmax unchanged because the common exponential factor cancels. Choosing the maximum makes every exponential argument nonpositive and at least one zero:

pi=ezi−m∑jezj−m,L=m+log⁡(∑jezj−m).p_i=\frac{e^{z_i-m}}{\sum_j e^{z_j-m}}, \qquad L=m+\log\left(\sum_j e^{z_j-m}\right).

For these scores, L≈1002.4076059644L\approx1002.4076059644. Unlike softmax, LSE changes when a common constant is added: it rises by that constant. More generally, m≤L≤m+log⁡Km\le L\le m+\log K, so LSE is a smooth approximation to the maximum. It is also useful for adding positive quantities already stored as logarithms, without first reconstructing their potentially enormous or tiny values.

Temperature changes the gaps

A common shift preserves every score difference. Dividing scores by a positive temperature TT changes those differences and generally changes the probabilities:
pi(T)=ezi/T∑jezj/T,pi(T)pj(T)=e(zi−zj)/T.p_i(T)=\frac{e^{z_i/T}}{\sum_j e^{z_j/T}}, \qquad \frac{p_i(T)}{p_j(T)}=e^{(z_i-z_j)/T}.

At T=2T=2, the example becomes approximately 0.186324, 0.307196 and 0.506480. The ranking stays the same, but the distribution is flatter. Temperature is therefore a modelling choice, whereas subtracting the maximum is an algebraically equivalent way to perform the same calculation.

For fictional logits 1000, 1001 and 1002, subtracting the maximum gives −2, −1 and 0, and probabilities 9.00%, 24.47% and 66.52%. Dividing logits by temperature 2 instead gives 18.63%, 30.72% and 50.65%. Log-sum-exp is approximately 1002.4076.
For fictional logits 1000, 1001 and 1002, subtracting the maximum gives −2, −1 and 0, and probabilities 9.00%, 24.47% and 66.52%. Dividing logits by temperature 2 instead gives 18.63%, 30.72% and 50.65%. Log-sum-exp is approximately 1002.4076.
Original fictional example: shifting preserves the probability distribution; temperature scaling changes it. The plotted bars use unrounded calculations.
INTERACTIVE EXPLORER

Stable softmax and temperature

Shift large scores safely and inspect probabilities and gradients.

Stay in log space when the loss needs logarithms

The maximum shift improves numerical stability; it does not eliminate every floating-point limitation. Very negative shifted scores can still produce underflow, and finite precision can erase small contributions.

For logits 0 and −1000, binary64 softmax rounds the second probability to zero even though its mathematical value is positive. Taking the logarithm afterwards gives negative infinity. A dedicated log-softmax calculation instead returns approximately 0 and −1000. Compute the log probabilities through the shifted expression

log⁡pi=(zi−m)−log⁡(∑jezj−m),\log p_i=(z_i-m)-\log\left(\sum_j e^{z_j-m}\right),
rather than taking the logarithm of a probability already rounded to zero. For an observed class cc, its negative log-likelihood is −log⁡pc=L−zc-\log p_c=L-z_c; the shifted form avoids constructing the probability first. A library's log-softmax or loss accepting logits implements this idea with numerical refinements. When processing a batch, normalise over the class axis for each observation, not across observations. SciPy's default axis is None, which normalises the whole input array.
The softmax and cross-entropy derivation develops the corresponding training gradient. A public preview is available; the full lesson requires membership.

A probability constraint is not a validation result

Normalisation alone does not establish probability calibration: predictions near 70% need not occur about 70% of the time. Temperature adjustments require validation too.

Softmax also imposes competition among its outputs. For multilabel classification, where several labels may hold together, separate sigmoid outputs can estimate labelwise marginal probabilities without making them sum to one. This does not establish independence between the labels.

Likewise, positive weights summing to one can parameterise a fully invested, long-only portfolio. Softmax alone supplies neither an investment objective nor expected returns, risks, costs or optimal allocations. The interpretation comes from the model and its evidence, not the normalisation formula.

Sources

Last updated
  • machine learning
  • classification
  • numerical stability
  • log probabilities
← Back to the glossaryBack to top ↑