Lesson 4.5 · 7 min read · about 45 min to master at a very slow pace

Cross-entropy / Log-loss

Cross-entropy is the negative average log-likelihood. Why it punishes confident wrong predictions so hard, how to compute it, and how it differs from accuracy as a measure.

Not startedJump to quiz

Binary cross-entropy (log-loss)

For each observation, only one term is active: if , and if . So the loss is "minus the log of the probability you gave to the truth".

012340.20.40.60.8predicted probability p of class 1lossconfidently wrongconfidently wrong
true y = 1: −ln ptrue y = 0: −ln(1 − p)
Loss is near zero when the model is confident and right, and shoots towards infinity when it is confident and wrong.
given to the truth0.990.90.50.10.01
0.0100.1050.6932.3034.605

Why use it to fit classifiers?

  • It is the maximum-likelihood loss for a yes/no (Bernoulli) outcome.
  • It rewards good probabilities, not just correct classes. Predicting 0.51 and 0.99 for a true positive both count as "correct" for accuracy, but cross-entropy prefers 0.99.
  • It is smooth and, for logistic regression, convex: easy to optimise.

Cross-entropy vs accuracy

AccuracyCross-entropy
UsesHard class predictionsPredicted probabilities
Depends on thresholdYesNo
Rewards confidenceNoYes (and punishes overconfidence)
Good for fittingNo (not smooth)Yes

Sign conventions

The log-likelihood has no minus sign and is maximised. Cross-entropy is and is minimised. Both are correct; don't mix them in one sentence.

Cheatsheet

Log-loss values

: 0.99→0.01, 0.9→0.105, 0.5→0.693, 0.1→2.303, 0.01→4.6.

Sign of the log-likelihood

is maximised (≤ 0). Cross-entropy is minimised (≥ 0).

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateFor a case with true label 1, what is the cross-entropy loss when the model predicts 0.8, and when it predicts 0.2?
Q2calculateFor a case with true label 0, the model predicts for class 1. What's the loss?
Q3calculateTwo cases: with predictions . What is the mean cross-entropy?
Q4conceptWhich loss does standard logistic regression minimise?
Q5interpretTwo classifiers have identical accuracy, but A has a much lower cross-entropy than B. What does that tell you?