Cross Entropy
Cross-entropy measures the expected negative log probability assigned by model distribution to events generated from true distribution . In classification, it is the mathematical form behind multinomial log loss and many deep-learning loss functions.
The distinction from entropy is which probabilities define the code. Entropy asks for the best average code length when the true distribution is known and used. Cross-entropy asks for the average code length when events still come from , but the code or model behaves as if were true. If puts too little probability on events that actually happen often, those common events receive overly long codes and the average length rises.
Defining math
For discrete distributions and on the same support,
It decomposes as
Since does not depend on the model, minimizing cross-entropy over also minimizes KL divergence. For one-hot class labels, puts all mass on the true class, so the loss reduces to .
Read the summand as “frequency under reality times surprise under the model.” The factor says how often outcome appears; the factor says how many bits the model’s probabilities would spend when that outcome appears.
For a supervised classifier, the same formula is usually written with ground-truth labels and predicted class probabilities. For one training example with classes, let
- be the ground-truth target for class ,
- be the model’s predicted probability for class .
Then the per-example classifier loss is
For a dataset of labeled examples, training minimizes the average loss
With one-hot labels, only for the correct class and for every other class, so the sum collapses to
That is the key machine-learning interpretation: cross-entropy does not punish the predicted label directly; it punishes the probability assigned to the true label. A confident wrong prediction gets a large loss because becomes very negative when the true-class probability is near zero.
Worked examples
Classifier loss
Suppose a three-class classifier sees one example whose true class is class 1. The one-hot target is
and the model predicts
The cross-entropy loss for this example is
Only the true class contributes because the other target entries are zero. If the same model assigned probability to the true class, the loss would become bits, even if the final argmax prediction were the only thing reported downstream.
Distribution and coding length
For soft labels or population distributions, the full sum remains active. Suppose the true event distribution is
but the model or code assigns probabilities
Cross-entropy asks how many bits are needed on average when events arrive from but are encoded using probabilities from :
This is the information-theoretic version of the same penalty. Probability mass from the real source distribution weights the surprise under . If underestimates events that happen often under , the expected code length rises.
The comparison to a uniform code makes the intuition clearer. If the code ignored the event frequencies and used
then
The uniform code is worse because it spends the same probability budget on all events even though the first event occurs 70 percent of the time. It gives that common event only probability , so the common case receives bits instead of the shorter bits assigned by . The nonuniform is still imperfect, but it is closer to and therefore has lower expected code length.
Both values are larger than because neither nor is exactly the data distribution. The extra cost is ; stable implementations combine log-softmax with the loss instead of separately computing probabilities, which connects this page to numerical stability.
Caveats
Cross-entropy heavily penalizes confident wrong probabilities. That is useful for probabilistic training, but noisy labels or uncalibrated targets can dominate the loss. If support differs and where , the cross-entropy is infinite.
References
- MacKay, Information Theory, Inference, and Learning Algorithms
- SciPy documentation:
scipy.special.rel_entr
Nav
Section — Mathematical Foundations