Basis Lab

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.

pk=ezk∑j=1Kezj=ezk−m∑j=1Kezj−m=e−mezke−m∑j=1Kezj=ezk∑j=1Kezjp_k = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}} = \frac{e^{z_k-m}}{\sum_{j=1}^{K} e^{z_j-m}} = \frac{e^{-m} e^{z_k}}{e^{-m} \sum_{j=1}^{K} e^{z_j}} = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}}

The cancellation

Write the shifted score as zj−mz_j-m. The numerator for class kk becomes ezk−m=e−mezke^{z_k-m}=e^{-m}e^{z_k}. Every term in the denominator becomes ezj−m=e−mezje^{z_j-m}=e^{-m}e^{z_j}, so the denominator is e−m∑jezje^{-m}\sum_j e^{z_j}. Dividing gives the chain above. The factor e−me^{-m} 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 mm and every finite set of scores: softmax has no distinguished origin on the score axis.

Take scores 2,1,02,1,0. The unshifted numerator for the first class is e2=7.389e^2=7.389, and the denominator is 7.389+2.718+1=11.1077.389+2.718+1=11.107, so p=7.389/11.107=0.665p=7.389/11.107=0.665. Subtract the maximum 22: the scores become 0,−1,−20,-1,-2, the exponentials become 1,0.3679,0.13531,0.3679,0.1353, the denominator is 1.50321.5032, and p=1/1.5032=0.665p=1/1.5032=0.665. The second class gives 2.718/11.107=0.2452.718/11.107=0.245 unshifted and 0.3679/1.5032=0.2450.3679/1.5032=0.245 shifted; the third gives 1/11.107=0.0901/11.107=0.090 and 0.1353/1.5032=0.0900.1353/1.5032=0.090. The last digits differ only because the decimals are rounded.

papb=ezaezb=eza−zb=e(za−m)−(zb−m)\frac{p_a}{p_b} = \frac{e^{z_a}}{e^{z_b}} = e^{z_a-z_b} = e^{(z_a-m)-(z_b-m)}

Equivalently, the probability vector depends only on the differences zj−zkz_j-z_k. 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 aa against class bb depend only on za−zbz_a-z_b, 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 m=max⁡jzjm=\max_j z_j. Then zj−m≤0z_j-m\le 0 for every jj, so ezj−m≤1e^{z_j-m}\le 1, and the largest term is exactly e0=1e^0=1. With KK classes the denominator is a sum of KK terms each at most 11, so the denominator lies between 11 and KK. No exponential in the calculation can overflow. Scores like 102,101,100102,101,100 have the same probabilities as 2,1,02,1,0, but a naive softmax computes e102e^{102}, which overflows in single precision. The shifted calculation computes 1,e−1,e−21,e^{-1},e^{-2} instead.

m=max⁡jzj,zj−m≤0,ezj−m≤1,1≤∑j=1Kezj−m≤Km = \max_j z_j, \qquad z_j-m \le 0, \qquad e^{z_j-m} \le 1, \qquad 1 \le \sum_{j=1}^{K} e^{z_j-m} \le K

Where exactness stops

In exact arithmetic the identity holds for any finite mm. 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 mm 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 log⁡∑jezj=m+log⁡∑jezj−m\log \sum_j e^{z_j} = m + \log \sum_j e^{z_j-m}, shifting every score by mm and adding mm back leaves the loss unchanged; computed together with the true-class score, the mm 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 course

Other questions people ask about this

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Read alongside