Cross Entropy Loss: 1 Formula Behind Better Classifiers

The cross entropy loss is the workhorse objective for classification, the way squared error is for regression. It measures how far a model’s predicted probabilities are from the truth, and it does so with a sharp bias: it barely penalises confident correct answers but punishes confident wrong ones brutally. That single property is what pushes a classifier to output well-calibrated probabilities, and it is why almost every neural network that ends in a softmax or sigmoid is trained against it.

cross entropy loss curve chart

📌
In one line. The cross entropy loss is the negative log of the probability the model assigned to the correct class — near zero when the model is confidently right, and huge when it is confidently wrong.

What is the cross entropy loss?

At heart it is beautifully simple: look at the probability your model gave to the correct answer, take its logarithm, and negate it. If the model said the right class was 99% likely, the log is almost zero and the penalty is tiny. If it said the right class was 1% likely, the log is a large negative number and the penalty is enormous. Averaged over every example, that is the cross entropy loss. It comes straight from information theory, where it measures the extra bits needed to encode the true labels using the model’s predicted distribution — but you can use it perfectly well thinking only of the negative-log-probability picture.

You will meet it under two other names. When there are just two classes it is usually called binary cross-entropy or log loss; with many classes it is called categorical cross-entropy. All three are the same idea applied to different numbers of outputs.

The cross entropy loss formula

L = − Σ yi log(pi)

Here yi is 1 for the true class and 0 for the rest, and pi is the probability the model assigned to class i. Because the true-label vector is all zeros except a single one, the sum collapses to a single term: minus the log of the probability on the correct class. That is the whole the loss for one example — everything else is just averaging and bookkeeping.

Try it: the cross-entropy calculator

Drag the slider to set the probability your model gave the correct class and watch the penalty. Notice how gentle it is near 1 and how it rockets upward as the probability falls toward 0.

Why the logarithm punishes confident mistakes

💡
The key intuition. The negative logarithm is nearly flat near a probability of 1 and shoots to infinity as the probability approaches 0. So a model that is 99% sure and right pays almost nothing, while a model that is 99% sure and wrong pays a fortune. This asymmetry is exactly what you want: it forces the network not just to be right, but to be honestly confident.

A worked example

Imagine a three-class problem where the true class is the first one. Two models both predict the right class, but with different confidence: model A outputs (0.7, 0.2, 0.1) and model B outputs (0.4, 0.35, 0.25). Both would score the same accuracy, yet their penalties differ sharply:

0.36Model A: −ln(0.7)
0.92Model B: −ln(0.4)
0.01If p=0.99
4.61If p=0.01

Accuracy cannot tell model A from model B, but the the loss rewards A for being more confident in the correct answer. That sensitivity to probability, not just the final label, is why it trains sharper, better-calibrated models than a bare accuracy target ever could.

Cross-entropy and softmax: the perfect pair

The loss almost always sits directly on top of a softmax (for single-label multi-class) or a sigmoid (for binary and multi-label). The pairing is not an accident: when you combine softmax with this loss, the gradient simplifies to the elegant “predicted probability minus true label,” which is numerically stable and fast. That is why frameworks fuse them into a single operation and ask you to pass raw scores, not probabilities.

Binary vs categorical

VariantClassesPairs with
Binary / log loss2 (or multi-label)sigmoid
Categorical3+ single-labelsoftmax
Sparse categorical3+ (integer labels)softmax

They differ only in how the labels are shaped and how many outputs there are; the underlying negative-log-probability idea is identical. Read the binary cross-entropy guide for the two-class case in depth.

Common mistakes to avoid

🚫
Three traps. Do not apply a softmax yourself and then feed the probabilities into a loss that also applies one — pass raw logits and let the framework do it once, or your gradients break. Do not use it on regression targets; it expects probabilities, not real numbers. And clip or smooth probabilities away from exactly 0 or 1, because the logarithm of zero is infinite and will blow up training.

Cross entropy loss in Python

import numpy as np

def cross_entropy(p_true_class):
    p = np.clip(p_true_class, 1e-12, 1.0)   # avoid log(0)
    return -np.log(p).mean()

print(cross_entropy(np.array([0.7, 0.9, 0.6])))   # 0.29

In PyTorch use nn.CrossEntropyLoss() (it applies softmax internally) or nn.BCEWithLogitsLoss() for the binary case; in Keras, categorical_crossentropy or binary_crossentropy. See the Wikipedia cross-entropy article for the information-theory background.

Putting it into practice

In day-to-day work the loss is both your training objective and a useful diagnostic. Because it reacts to probabilities rather than hard labels, a model can improve its the loss for several epochs even while its accuracy sits still — it is becoming better calibrated before it starts flipping predictions, so watch the loss, not just accuracy, to judge progress. If the training loss falls smoothly while the validation loss turns upward, you are overfitting and it is time to stop or regularise. A sudden spike to a huge value almost always means a probability hit zero somewhere, which points to a missing clip or an unstable learning rate. And when you finally report results, remember that a good loss and a good accuracy are related but not identical: a model can be accurate yet poorly calibrated, and it is precisely that calibration gap the the loss is designed to expose and close.

From information theory to training

The idea has deep roots in information theory, where cross-entropy measures the average number of bits you would need to encode events drawn from one distribution using a code built for another. In machine learning the “true” distribution is the labels and the “other” distribution is the model’s predictions, so minimising the quantity means making the model’s predicted distribution match reality as closely as possible. It is closely tied to a cousin called Kullback–Leibler divergence, which measures how far apart two distributions are; when the true labels are fixed, minimising one is equivalent to minimising the other. You do not need any of this theory to use the objective effectively, but it explains why the metric behaves the way it does: it is fundamentally a measure of surprise, and a confident wrong answer is maximally surprising, which is exactly why it is punished so hard. Maximum-likelihood estimation, the statistical principle behind fitting most models, also reduces to this same minimisation, which is why the objective shows up almost everywhere probabilities are learned.

Reading the curve during training

Because the objective reacts to probabilities rather than to hard labels, it is a more sensitive progress signal than accuracy, and reading it well saves a lot of wasted training time. Early on it typically falls quickly as the model stops making wildly overconfident mistakes, then settles into a slow grind as it fine-tunes calibration. A validation curve that flattens and then creeps upward while the training curve keeps dropping is the classic signature of overfitting, and it is your cue to stop, add regularisation, or gather more data. Sudden spikes usually mean a probability collapsed to zero or the learning rate is too high. It also pays to remember that a lower value is not automatically a better model for your purpose: a network can achieve an excellent score by being beautifully calibrated on the common classes while quietly failing on a rare but important one, so always read it next to a class-aware metric such as recall on the class you care about.

Frequently asked questions

What is the the loss in simple terms?
It is the negative logarithm of the probability your model assigned to the correct class. It is tiny when the model is confidently right and very large when it is confidently wrong.
Why is cross-entropy used instead of accuracy for training?
Accuracy only looks at the final label and has no useful gradient. Cross-entropy reacts to the predicted probability, so it gives a smooth signal that pushes the model to be confidently correct and well calibrated.
Is log loss the same as cross entropy?
Yes. Log loss is the name commonly used for binary cross-entropy, and it is the same negative-log-probability idea applied to a two-class problem.
What does cross-entropy pair with?
Softmax for single-label multi-class problems and sigmoid for binary or multi-label problems. Frameworks usually fuse the activation and the loss for numerical stability.
Why does my the loss become infinite?
Because a predicted probability reached exactly zero for the true class and log(0) is infinite. Clip probabilities into a range like 1e-12 to 1, or pass logits to a fused loss that handles this safely.
Scroll to Top