Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Computing Cross-Entropy Loss for a Classification Example

Welcome back. In the previous lesson, you converted logits into probabilities with softmax. That tells you what a classifier predicts, but not how well its prediction matches the known answer. Today you’ll compute cross-entropy loss for one example: take the probability assigned to the correct class and turn it into a number that is small for a strong prediction and large for a poor one.

Allow about 35 minutes for the video, reading, and calculations.


From a prediction to a loss

Suppose a model assigns probabilities , , and to cat, dog, and horse. If the image really shows a cat, the model assigned the correct class probability . If it shows a horse, that probability is . The same prediction can therefore have a different loss depending on the true label.

Watch the single-example calculations in StatQuest’s Neural Networks Part 6: Cross Entropy. Notice that the presenter selects the probability of the observed flower species—not simply the largest probability—before calculating the loss.

Neural Networks Part 6: Cross Entropy

StatQuest with Josh Starmer uses three-class flower predictions to show which probability enters a single example's cross-entropy calculation.

Watch the examples, from the first setosa record through the third record's loss. Pay particular attention to the explanation beginning around 00:02:53 of why the terms for the two incorrect classes disappear from the formula.

The calculation uses the natural logarithm, written here. For a correct-class probability , the loss is

For example, if the correct class receives probability , then . If it receives only , then . A calculator’s ln key—or np.log in NumPy—computes the natural logarithm.

Why the minus sign? The logarithm of a probability between zero and one is negative. Negating it makes the loss nonnegative. As the correct-class probability approaches one, the loss approaches zero; as that probability approaches zero, the loss grows without bound. A probability of exactly zero would make its logarithm undefined, though mathematical softmax outputs from finite logits are greater than zero.


Why this is called cross-entropy

The compact rule above is a special case of a formula for comparing two probability distributions. Let be the model’s probability for class , and let represent the target. For a single example with one correct class, the target is one-hot: it has a at the correct class and elsewhere. Cross-entropy is

Only the correct class has ; every other term is multiplied by zero. Thus the sum becomes . The other predicted probabilities are not irrelevant: because softmax probabilities share a denominator and sum to one, increasing probability assigned to an incorrect class leaves less probability for the correct one.

Read the single-example explanation in CS231n’s Deep Learning for Computer Vision. Its symbol denotes the logits, and is the logit at the correct class.

CS231n Deep Learning for Computer Vision

Read CS231n's explanation to connect the softmax calculation from last lesson with the negative log of the correct-class probability.

In the subsection “Compute the loss,” begin at the loss explanation. Finish the sentence about loss approaching zero, then stop before the paragraph beginning “Recall also that the full Softmax classifier loss.” Focus on the formula for one example; dataset-wide averaging and regularization are not needed yet.


Calculate one example from logits

The diagram below follows one example from raw class scores to probabilities and loss. Focus on its lower, green-bordered path; the upper path computes a different kind of loss.

A three-class classifier produces logits \((-2.85,\,0.86,\,0.28)\). The lower path applies softmax, then takes the negative natural logarithm of the probability for the correct class, the third entry.

The label uses zero-based indexing: class is the third entry. Its logit is , not the largest logit. Following the diagram’s rounded probabilities gives

We can check how softmax produced the probability needed for that last step. As in the previous lesson, subtract the largest logit, , from all three scores without changing the probabilities:

Exponentiating gives approximately . Divide the correct class’s value by the sum of all three:

Then . Small differences in the final decimal come from rounding. Notice that taking the logarithm of the correct logit, , would be a different and incorrect calculation: cross-entropy takes the logarithm of the correct probability.


What the number tells you

A few values establish the scale:

Probability assigned to the correct class Cross-entropy loss

Loss provides more information than whether the top-ranked class is correct. Predictions assigning and to the true class receive different losses, even if both rank it first. Likewise, the model in the diagram ranks the second class first, but the loss is calculated using the third class because that is the known answer.

For this lesson, the complete procedure is: obtain the softmax probabilities, identify the true class, select its probability, and compute its negative natural logarithm. Do not substitute the target’s one-hot value of for the model’s predicted probability—the target tells you which entry to select, not what probability the model assigned.


Key takeaways

For one classification example, cross-entropy is . The correct label selects an entry from the predicted probability distribution. High probability on that entry produces low loss; low probability produces high loss. The same calculation will later apply when a model assigns probabilities to possible next tokens in text.

Next, you’ll begin tracing how a change in one quantity affects another using the chain rule.

Can't find a good explanation? Sign up and we'll make it for you

Sign up