Part IV — Supervised Learning: Regression & Linear Classifiers · Chapter 7

Binary cross-entropy loss

Hook

This example's true answer is "yes." How much should it cost your model to have said "almost certainly not"?

Intuition

Drag the point — it's your model's predicted probability that this true-positive example is positive. Near 1 the loss barely moves. Down near 0, it shoots upward without any sign of leveling off.

Formalize

That's cross-entropy loss. For one example with true label yy and predicted probability pp

L=[ylogp+(1y)log(1p)]L = -\big[y\log p + (1-y)\log(1-p)\big]
  • LL — the cross-entropy loss for one example.
  • yy — the true label, exactly 00 or 11.
  • pp — the model's predicted probability that the example is positive.
  1. Only one term is ever active

    Since yy is exactly 0 or 1, exactly one of the two terms in the brackets is ever nonzero — the loss only ever looks at the probability assigned to the true label.

  2. Confidently wrong predictions blow up

    A probability approaching 0 for the correct answer sends logp\log p toward -\infty — and the leading minus sign turns that into a loss with no ceiling at all.

Play
L = 0.69 (squared error would say 0.25)

Watch the comparison: squared error tops out at 11 no matter how wrong the prediction gets, but cross-entropy keeps climbing. A model that's confidently wrong gets punished far harder than one that's just wrong.

Worked example

True label y=1y=1, predicted p=0.1p=0.1 — confidently wrong:

  1. Compute the cross-entropy loss
    L=log(0.1)2.303L = -\log(0.1) \approx 2.303
  2. Compare it to squared error

    Squared error for the same case is only (10.1)2=0.81(1-0.1)^2 = 0.81 — cross-entropy is already nearly three times larger, and would keep growing the more confident the wrong prediction became.

Checkpoint

This example’s true label is 1. Drag p until the loss reads (approximately) 0.05.

L = 0.69
Drag the point to try it
Summary
L=[ylogp+(1y)log(1p)]L = -\big[y\log p + (1-y)\log(1-p)\big]

Cross-entropy is built for probability outputs specifically: it's small when a prediction is confidently correct, and unboundedly large when a prediction is confidently wrong. That's exactly the signal gradient descent needs to fix a badly wrong classifier fast.