A spam filter doesn't just say "spam" or "not spam" — underneath, it outputs a probability, like "97% sure this is spam." To train that filter, we need a way to score how good each probability guess was, once we find out the real answer. Squared error — the tool we use for plain numbers — turns out to be the wrong ruler for probabilities: it doesn't punish a wildly overconfident wrong answer nearly hard enough.
What we actually want is a loss that is gentle when the model is confidently right,
and brutal when it's confidently wrong — so the safest strategy is always to be
honest about your uncertainty. That loss is called cross-entropy (also known as
log loss). For a single example with true label
It looks like two terms added together, but really it's a switch: only one term is ever "on" for a given example, and which one depends on the true label. Think of it like a strict exam marker: the marker doesn't care how eloquently you argued your answer — only how much probability you were willing to stake on the option that turned out to be correct. Stake it all on the right answer and you pay almost nothing; stake it all on the wrong one and the bill is severe.
Look at what happens to each half of the formula depending on
Either way, the formula boils down to one simple English sentence: "take the negative log
of the probability the model assigned to the class that actually happened." Since
probabilities live between
The best way to feel the shape of this formula is to plug in real numbers. Suppose the true label is
always
The same pattern holds in mirror image when the true label is
Run the numbers yourself:
The ordering — small, then moderate, then huge — is exactly the incentive we want to give a model during training: don't just try to be right, try to be calibrated, and never bluff.
Pick the true label and watch the loss as the predicted probability slides from
A model that says "100% spam" about a real, important email should be punished without
mercy — cross-entropy's blow-up toward infinity does exactly that, discouraging the model from ever
claiming total certainty unless it has earned it. Averaged over every example in the training set
and minimised by
Compare that to squared error,
Everything so far has been the loss for one example. In practice a model is trained on a whole batch of examples at once, and what actually gets minimised is the average loss across every one of them. Suppose a tiny dataset of four emails has these true labels and predictions:
The dataset's overall loss is the mean of those four numbers:
Everything here has been the binary version, for a yes/no question like
"spam or not spam." Real classifiers often have to choose between many classes at once — which of
10 handwritten digits, which of 1000 animal species. That's the job of
Look again at
Real implementations dodge this quietly: they clip every predicted probability to
stay just inside the open range, e.g. never letting
Yes — and it happens constantly. Accuracy only asks "did you pick the right class?" — a yes/no question. Cross-entropy asks "how confident, and how well-calibrated, was your probability?" — a much finer-grained question. A model can keep every single one of its predictions on the correct side of the decision boundary (accuracy frozen, unchanged) while quietly tightening its probabilities — turning a shaky "60% sure" into a firm "92% sure" on examples it was already getting right. Accuracy doesn't move a millimetre, but cross-entropy loss drops, because the model has become better calibrated, not just better at picking sides.
This is exactly why cross-entropy, not accuracy, is the number used to drive training: it keeps giving useful feedback long after accuracy has flatlined.
"Cross-entropy" isn't a made-up machine-learning word — it was borrowed wholesale from information theory, the field Claude Shannon founded in 1948 to figure out how efficiently messages could be squeezed down a telegraph wire. Shannon defined the "entropy" of a message as how many bits of surprise it carries on average: a message you could have predicted perfectly carries zero surprise (zero bits), while a wildly unexpected message carries a lot. Cross-entropy extends that idea to compare two probability distributions — the one you predicted versus the one that actually happened — and that is precisely what a classifier's loss needs to measure.
It's also worth asking: why not just train a model by directly counting how many answers it gets wrong (its error rate)? Because "count the mistakes" is a staircase function — flat, then a sudden jump, then flat again — with zero slope almost everywhere. Gradient descent needs a smooth hill to walk down, and cross-entropy, unlike a raw mistake-count, changes gradually as the predicted probabilities nudge up or down. That smoothness is the unglamorous reason cross-entropy — not accuracy — is the number actually staring back at the optimiser during training for nearly every classifier on Earth.