Label against prediction

Sample and training

Cross-entropy loss

A classifier outputs a probability for every class (ŷ). Training needs one number saying how bad that is, given the right answer (y). Cross-entropy is that number: L = −Σ yi ln ŷi.

  • Confident and right (ŷ of the true class near 1): L near 0.
  • Unsure (10% on every class): L = ln 10 = 2.303.
  • Confident and wrong (ŷ of the true class near 0): L grows without limit.

The three runs

Each is a small network trained for 25 epochs, with its predictions for 10 samples saved along the way: handwritten digits (MNIST), small photos (CIFAR-10, a harder task) and next-word prediction on a tiny corpus (Text). Text labels are soft: “the cat sat on” is followed by “the” half the time and “a” the other half.

Try this

  1. MNIST: press Train and watch the loss fall. Which digit stays hardest?
  2. CIFAR-10: find a sample whose loss goes up late in training. Is its top guess right?
  3. Text: how low can the loss go? Compare it with the dashed floor in the Training tab.
  4. Manual: put 90% on a wrong class. How large is the loss?
L = −Σi yi ln ŷi

For a one-hot label (class k): L = −ln ŷk. In nats; divide by ln 2 for bits.

Why ln?

Minimising cross-entropy is maximising the likelihood the network gives the training labels: the log turns the product of probabilities into a sum. Its gradient through softmax is simply ŷ − y.

ŷ of the true classL
10
0.50.693
0.12.303
0.014.605