Answers · The mathematics underneath
Why does cross entropy loss only use the probability of the correct class?
In shortBecause 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.
The loss only uses the probability of the correct class because a definite label is a one-hot vector: it puts 1 on the observed class and 0 on every other. In the sum over classes every term except the true one is multiplied by 0, so the live term is the negative log of the probability assigned to the correct class.
For a single example the outer average is irrelevant. Take three classes, an observed class 2, target and model . The inner sum has three terms, but two of them carry a target of zero. Using natural logarithms, the loss on that example is
| class | target t | model p | t x (-log p) |
|---|---|---|---|
| 1 | 0 | 0.25 | 0 |
| 2 | 1 | 0.60 | 0.5108 |
| 3 | 0 | 0.15 | 0 |
The zeros come from multiplication, not from the wrong-class probabilities being small. The model could have put 0.25 and 0.15 elsewhere, in any split that leaves the true class at 0.60, and the loss on this example would not move. The value 0.5108 is the entire loss: it is , since .
The common misreading is that because the formula sums over all classes, every predicted class probability contributes to the loss. It does not. The target vector decides which terms survive, and for a hard label all but one of its entries are exactly zero. The wrong-class probabilities are present in the expression, but they are multiplied by zero, not added with small weights. That is why an implementation can take a vector of predictions and an integer label: the integer is the one-hot target with the zeros omitted. A sum over all classes is still the right general form because it also covers soft targets; it is not a claim that all classes matter for a hard label.
There is a real consequence. Suppose the true class gets 0.60. Model A gives and model B gives . Both have loss . The loss cannot distinguish a near miss from an absurd one, because it never reads the 0.35 or the 0.05. If that distinction matters, it has to enter through the targets or through another term.
When the target is not one-hot
The collapse depends on the target being certain. With label smoothing, distillation, or genuinely uncertain labels, several entries of are nonzero and every corresponding class contributes. For target and model the loss is
All three terms are live there, and the loss now depends on how the model distributed probability over the wrong classes. The one-live-term form is the special case, not the definition.
Two classes and the uniform start
For two classes the general expression is still the loss. Write the model's two numbers as and , and the target's as and . Then
If , the second term vanishes and . If , the first term vanishes and . Binary cross entropy is not a different loss; it is the same sum with two classes and the second probability written as one minus the first.
Worth keeping
- With a one-hot target, cross entropy on an example collapses to the negative log of the probability assigned to the true class.
- The loss is indifferent to how probability is spread over the wrong classes when the label is certain.
- An untrained uniform model over K classes has loss log K, so a ten-class run should begin near 2.3026.
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 The Loss Itself, 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 subtracting the max from the logits not change softmax?Subtracting the largest score is an exact rewriting of softmax, not a change to the distribution: the same factor cancels above and below.
- 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.