What it measures
Cross-entropy is the average number of bits you waste by encoding data drawn from one distribution using a code built for another.
For classification the true distribution puts all its mass on the correct class, and the other distribution is whatever the model predicted.
- : the true probability of class , which is 1 for the right class and 0 for the rest.
- : what the model predicted for that class.
- : the number of classes.
Why it collapses to one term
Because is one-hot, every term in that sum where is zero contributes nothing.
- : the index of the correct class.
So the loss for one example depends on exactly one number: the probability the model gave the right answer.
Predict 0.9 and the loss is 0.105. Predict 0.1 and it is 2.303. Predict 0.01 and it is 4.605.
The penalty grows without bound as the prediction approaches zero, which is what makes it punish confident mistakes so much harder than uncertain ones.
Over a whole batch
The mean, not the sum, so the learning rate does not have to change when the batch size does.
The gradient, and why it is so clean
Composed with a softmax, the derivative of this loss with respect to the logits is not the matrix softmax alone would give you.
The error is simply the predicted probability minus the true one.
That is the reason the two are fused into a single operation in every framework: computing them separately means building a matrix that immediately cancels.
One example, start to finish
- The network produces logits, one per class.
- Softmax turns them into probabilities.
- Look up the probability of the correct class.
- Take its negative logarithm, and that is the loss.
- Backward, the gradient at the logits is just prediction minus target.
The two things that go wrong
A predicted probability of exactly zero makes the logarithm negative infinity, so implementations either add a small epsilon or work in log space throughout.
The second is passing already-softmaxed probabilities into a function that expects logits, which applies softmax twice and quietly flattens every gradient.