Gradient LabStart the lesson

Lessons › Chapter 8: Neural networks

Cross-entropy loss

Classifiers output probabilities. To train them we need a loss that judges how much probability went to the right answer. The standard choice is cross-entropy.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Confidently wrong

The cheque reader was 99% sure about a digit that turned out wrong. Maya wants that punished hard.

Concept

Grading a probability

Classifiers output probabilities. To train them we need a loss that judges how much probability went to the right answer. The standard choice is cross-entropy.

Build

Confident and right: tiny loss

If the model gave 90% to the right answer, the loss is just 0.11. Even at 50%, it’s a modest 0.69.

Key idea

Confident and wrong: huge loss

If the model gave the right answer only 1%, the loss shoots up to 4.6. Cross-entropy punishes confident mistakes very hard, which pushes the model to be honest about uncertainty.

Real life

In real life: grading weather forecasts

Forecasters are scored the same way. Saying “99% sunny” on a day it pours is punished far more than saying “60% sunny”. Being confidently wrong is the worst outcome, for forecasters and for models.

Quick check: Which prediction gets the biggest cross-entropy loss?

Loss is −log(p): the smaller the probability on the truth, the bigger the loss.

Pseudocode

p = the model's probability for the correct answer
loss = −log(p)

p = 0.9   → loss 0.11    # confident and right: tiny loss
p = 0.5   → loss 0.69
p = 0.01  → loss 4.6     # confident and wrong: huge loss

Key terms

cross-entropy
A loss for classification: small when the model is confident and right, huge when confident and wrong.