Answers · The mathematics underneath
Why does subtracting the max from the logits not change softmax?
In shortSubtracting the largest score is an exact rewriting of softmax, not a change to the distribution: the same factor cancels above and below.
Subtracting the maximum is an exact rewriting of softmax, not an approximation: every exponential is multiplied by the same factor, and that factor cancels between numerator and denominator. The probabilities are unchanged. What changes is only the size of the intermediate numbers, which is why implementations do it.
The cancellation
Write the shifted score as . The numerator for class becomes . Every term in the denominator becomes , so the denominator is . Dividing gives the chain above. The factor appears once on top and once on the bottom, so it cancels. Nothing else is left to change. This is an identity for every real and every finite set of scores: softmax has no distinguished origin on the score axis.
Take scores . The unshifted numerator for the first class is , and the denominator is , so . Subtract the maximum : the scores become , the exponentials become , the denominator is , and . The second class gives unshifted and shifted; the third gives and . The last digits differ only because the decimals are rounded.
Equivalently, the probability vector depends only on the differences . Add the same constant to every score and each difference is unchanged; subtract the same constant and each difference is unchanged again. The common part of the scores is a direction softmax cannot see. That is why a network can let the absolute level of its scores drift without changing its predictions, and why the final layer has one redundant direction in parameter space. It also tells you what the max subtraction is not: it is not a normalisation step that alters the model's confidence, because confidence is carried by the gaps between scores, not by their common level.
The natural guess is that subtracting a number from every score must lower each probability or change their ratios. It cannot change a ratio: the displayed identity says the odds of class against class depend only on , and a common shift leaves that difference unchanged. If every pairwise ratio is unchanged, the whole distribution is unchanged. The subtraction is not a modelling choice and it does not alter what the model believes. It is a numerical rewrite. The maximum is convenient, not essential; any constant would preserve the distribution.
Why the maximum
Any constant works in exact arithmetic, but the maximum has a special numerical property. Let . Then for every , so , and the largest term is exactly . With classes the denominator is a sum of terms each at most , so the denominator lies between and . No exponential in the calculation can overflow. Scores like have the same probabilities as , but a naive softmax computes , which overflows in single precision. The shifted calculation computes instead.
Where exactness stops
In exact arithmetic the identity holds for any finite . In floating point, the shifted and unshifted versions can disagree in the last digits because each exponential is rounded separately; that is rounding error, not a change in the distribution. A bad shift can still fail: if is smaller than the largest score, some exponent remains positive and may overflow, so the useful choice is the actual maximum. At the other end, very negative shifted scores underflow to zero, which gives those classes probability zero in the computation; their true probabilities are below the smallest representable positive number, so the effect on the returned distribution is negligible. The operation is safe because the large common factor is removed before it is ever formed, not because a new distribution is being defined.
The same cancellation is what makes cross entropy over softmax stable. Since , shifting every score by and adding back leaves the loss unchanged; computed together with the true-class score, the cancels. The loss becomes a log of a sum of exponentials of non-positive numbers minus a shifted score, so no division by a tiny number and no logarithm of zero appears. That is the practical payoff of the invariance: the distribution is mathematically the same, and the arithmetic no longer has to handle exponentials of enormous scores.
Worth keeping
- Subtracting the largest score leaves softmax unchanged because the same factor multiplies every exponential and cancels.
- The maximum is chosen only so the largest exponential becomes one and overflow is avoided.
- Any constant shift would preserve the distribution, so only differences between scores affect the probabilities.
Where this goes next
This question is one step of The Mathematics of Cross Entropy, a course that works through its subject from the beginning. The page that teaches this part of it is Where the Probabilities Come From, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why can't cross entropy loss go below the entropy of the labels?Cross entropy cannot fall below the uncertainty already in the labels because it is that uncertainty plus a non-negative distance from the model to the labels.
- What does a perplexity of 10 actually mean?A perplexity of 10 means the model's average loss is about 2.30 nats per prediction, equivalent to a uniform choice among ten equally likely options, not a 10 percent accuracy.
- What does temperature scaling actually change in a model's probabilities?It divides every score by one positive number before the softmax, so the probabilities become sharper or flatter while the ranking and top prediction stay the same.
- Why does cross entropy loss only use the probability of the correct class?Because a definite label puts all the target weight on the correct class and none on the others, so every other term is multiplied by zero.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.