On this page
Many programs that sort images or words into categories end with two steps: a softmax turns raw scores into probabilities, and a cross-entropy loss measures how much probability went to the right answer. You can check every number below with a calculator.
What does the softmax function do?
Stanford's CS231n notes on linear classification define the softmax as pᵢ = e^zᵢ / Σⱼ e^zⱼ, a function that squashes any list of real scores into values between zero and one that sum to one. The exponential makes every score positive, and dividing by the total makes them add up to 1.
Take three classes, say cat, dog and bird, with scores z = (2, 1, 0.1):
Exponentiate: e² = 7.389, e¹ = 2.718, e^0.1 = 1.105.
Add them: 7.389 + 2.718 + 1.105 ≈ 11.212.
Divide each by the total: 7.389/11.212 = 0.659, 2.718/11.212 = 0.242, 1.105/11.212 = 0.099.
Rounded, that is (0.66, 0.24, 0.10). Cat's score is twice dog's, yet its probability is e¹ ≈ 2.7 times dog's: softmax responds to differences between scores, not ratios.
Why subtract the maximum score before softmax?
Add the same number c to every score and nothing changes: e^(zᵢ + c) = e^c · e^zᵢ, and the factor e^c appears on the top and the bottom, so it cancels. The CS231n notes use this freedom for safety: shift the scores so that the highest one is zero.
Our example becomes (0, −1, −1.9). Now e⁰ = 1, e⁻¹ = 0.368 and e^−1.9 = 0.150, the total is 1.517, and the probabilities are again 0.659, 0.242 and 0.099.
Why bother? The same notes use scores (123, 456, 789) and mark the direct calculation as a "potential blowup". The number e^789 has 343 digits before the decimal point, far more than the largest value a standard floating-point number can hold (Python's sys.float_info lists that maximum as DBL_MAX). After the shift the scores are (−666, −333, 0): the largest exponential is exactly 1, and nothing can overflow.
The PyTorch log_softmax documentation adds that computing log(softmax(x)) as two separate steps is "slower and numerically unstable".
What is cross-entropy loss, and why −log p?
For one example whose correct class is c, the cross-entropy loss is L = −log p_c, which the CS231n notes describe as minimising "the negative log likelihood of the correct class". That is maximum likelihood estimation in action: prefer the model that makes the observed answers most probable.
With our scores, the three possible losses (natural log) are:
correct answer cat: −ln 0.659 = 0.417
correct answer dog: −ln 0.242 = 1.417
correct answer bird: −ln 0.099 = 2.317
The losses differ by exactly the gaps between the scores, because −log p_c = log Σⱼ e^zⱼ − z_c. A confident wrong answer costs a lot, and the cost grows without limit as p_c heads towards 0. A model that gives each class 1/3 pays ln 3 ≈ 1.10, a handy check at the start of training.
The name comes from information theory. The same notes write cross-entropy as H(p, q) = −Σ p(x) log q(x); when the true distribution puts all its weight on one class, only −log q_c survives, and minimising it is the same as minimising the Kullback–Leibler divergence. If entropy itself is new, start with why a fair coin carries 1 bit.
Why is the gradient of softmax with cross-entropy just p − y?
Write the loss in terms of the scores: L = −z_c + log Σⱼ e^zⱼ. Differentiate with respect to any score z_k. The second term gives e^z_k / Σⱼ e^zⱼ, which is p_k. The first gives −1 if k is the correct class and 0 otherwise. So ∂L/∂z_k = p_k − y_k, where y is the one-hot vector of the right answer.
The CS231n neural network case study derives the same result and calls the expression "elegant and simple".
With cat correct, y = (1, 0, 0), so the gradient is (0.659 − 1, 0.242, 0.099) = (−0.341, 0.242, 0.099). Read it as instructions: gradient descent moves against the gradient, so it raises cat's score and lowers dog's more than bird's. The entries always sum to zero, because p and y each sum to 1.
This is why libraries fuse the pair. PyTorch's CrossEntropyLoss expects "unnormalized logits" and is equivalent to LogSoftmax followed by NLLLoss.
A five-step check you can do by hand
Pick three scores and subtract the largest from all of them.
Exponentiate, add and divide; confirm the probabilities sum to 1.
Compute −ln p for the correct class; confirm it equals ln(Σ e^z) − z_c.
Write the gradient p − y; confirm it sums to zero.
Raise the correct score by 0.1 and recompute the loss. It should fall by about 0.1 × (1 − p_c), which is 0.034 for cat.
If the exponential and logarithm rules behind these steps feel shaky, the Fermi Concept Library gives each concept in mathematics and computing its own lesson. To practise derivatives like this one, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



