Intuition What this page is for
The parent note gave you the formula and one clean gradient. This page throws every kind of input at it: both labels, confident/unsure predictions, the two dangerous edges (y ^ → 0 and y ^ → 1 ), a full batch, a real-world story, and an exam-style trap. If you can work all of these, no log-loss question can surprise you.
Recall the one formula we lean on the whole way (from the parent and Logistic Regression ):
ℓ = − [ y log y ^ + ( 1 − y ) log ( 1 − y ^ ) ]
y is the true label (either 0 or 1 ). y ^ ∈ ( 0 , 1 ) is the model's predicted probability that the label is 1 , produced by the Sigmoid function . "log " here means natural log ln (base e ), so our units are nats . Here ℓ is the loss of one example; later (Ex 5) we average many such ℓ values into a batch cost we will call J .
Every log-loss situation is one cell in this table. The columns are the model's prediction; the rows are the true label. The extra rows below are the special/degenerate/word cases.
Case class
What makes it special
Example #
y = 1 , y ^ high (correct + confident)
best case, loss near 0
Ex 1
y = 1 , y ^ low (wrong + confident)
punished, loss large
Ex 2
y = 0 , y ^ low (correct + confident)
mirror of Ex 1, uses the other term
Ex 3a
y = 0 , y ^ high (wrong + confident)
mirror of Ex 2
Ex 3b
y ^ = 0.5 (maximally unsure)
baseline, same loss for either label
Ex 4
Full batch (average of many single losses)
how single losses combine into one number
Ex 5
Degenerate y ^ → 0 or → 1
limiting behaviour, NaN danger, clipping
Ex 6
Real-world word problem
translate a story into y , y ^
Ex 7
Exam twist: log 2 vs ln , and a gradient
units + the ( y ^ − y ) update
Ex 8
The figure below plots the two "surviving-term" curves so you can see every cell before we compute it.
Figure s01 — two chalk curves on a dark board. The blue curve − log y ^ rises from 0 at the right (y ^ = 1 ) to ∞ at the left (y ^ = 0 ): this is the loss when the truth is y = 1 . The pink curve − log ( 1 − y ^ ) is its mirror, rising to ∞ at the right: the loss when y = 0 . A yellow dashed line marks the ln 2 = 0.693 baseline, and the two curves cross there at y ^ = 0.5 . Labelled dots mark the Ex 1, Ex 2 and Ex 3b heights.
Intuition Reading the figure
The blue curve is − log y ^ : the loss you pay when the truth is y = 1 . The pink curve is − log ( 1 − y ^ ) : the loss when the truth is y = 0 . Each example below is literally reading a height off one of these two curves. Notice both curves are near 0 where the model agrees with the truth, and shoot to ∞ where it confidently disagrees.
Worked example Ex 1 — Cell:
y = 1 , confident & correct
True label y = 1 . Model predicts y ^ = 0.95 .
Forecast: will the loss be closer to 0 , to 0.7 , or above 2 ? Guess before reading.
Only the y log y ^ term survives because 1 − y = 0 .
Why this step? Multiplying the second term by ( 1 − y ) = 0 deletes it. One term per example — always.
ℓ = − log ( 0.95 ) .
Why this step? Direct substitution into the surviving term.
ℓ = − ( − 0.0513 ) = 0.0513 nats.
Why this step? log 0.95 is a small negative; the leading minus flips it positive.
Verify: 0.05 is tiny and positive — matches "correct + confident = near-zero loss", and it sits low on the blue curve of the figure near y ^ = 0.95 . ✓
Worked example Ex 2 — Cell:
y = 1 , confident & WRONG
True label y = 1 . Model predicts y ^ = 0.05 .
Forecast: compared to Ex 1 (same label, prediction flipped), how many times bigger is the loss?
Again only y log y ^ survives (y = 1 ).
Why this step? The true label decides which term lives, not the prediction.
ℓ = − log ( 0.05 ) .
Why this step? Substitute the (now tiny) probability the model gave the true class.
ℓ = − ( − 2.996 ) = 2.996 nats.
Why this step? log of a small number is a large negative → large positive loss.
Verify: 2.996/0.0513 ≈ 58.4 × bigger than Ex 1 — confidently wrong is punished far harder than confidently right is rewarded. This is the far-left of the blue curve climbing toward ∞ . ✓
Worked example Ex 3 — Cells 3a & 3b:
y = 0 correct AND y = 0 wrong (the mirror term)
Now the truth is y = 0 . Compute for (3a) y ^ = 0.05 (correct) and (3b) y ^ = 0.95 (wrong).
Forecast: which term of the formula survives now, and which of (3a)/(3b) should hurt?
With y = 0 , the first term y log y ^ = 0 ; the second term ( 1 − y ) log ( 1 − y ^ ) survives.
Why this step? y = 0 kills the first term. The model's belief in the true class is now 1 − y ^ .
(3a) ℓ = − log ( 1 − 0.05 ) = − log ( 0.95 ) = 0.0513 nats.
Why this step? Truth is class 0; the model gave class 0 a probability of 1 − y ^ = 0.95 . Great.
(3b) ℓ = − log ( 1 − 0.95 ) = − log ( 0.05 ) = 2.996 nats.
Why this step? The model gave the true class only 0.05 probability. Confident and wrong.
Verify: (3a) equals Ex 1's answer and (3b) equals Ex 2's — perfect mirror symmetry. On the figure, (3a)/(3b) are read off the pink curve at y ^ = 0.05 and y ^ = 0.95 . ✓
Worked example Ex 4 — Cell: maximally unsure,
y ^ = 0.5
Model refuses to commit: y ^ = 0.5 . Compute the loss for both possible truths.
Forecast: does the loss depend on whether y = 0 or y = 1 here?
If y = 1 : ℓ = − log ( 0.5 ) = 0.693 nats.
Why this step? y = 1 keeps the log y ^ term; substitute y ^ = 0.5 .
If y = 0 : ℓ = − log ( 1 − 0.5 ) = − log ( 0.5 ) = 0.693 nats.
Why this step? y = 0 keeps the log ( 1 − y ^ ) term, and 1 − 0.5 = 0.5 too — so both surviving terms equal − log 0.5 and the label doesn't matter.
So a coin-flip predictor always pays ln 2 ≈ 0.693 per example.
Why this step? This is the baseline from the parent note — any useful model must beat it.
Verify: ln 2 = 0.6931 ; and the blue and pink curves in the figure cross exactly at y ^ = 0.5 at height 0.693 . Any model scoring J > 0.693 is worse than guessing. ✓
Worked example Ex 5 — Cell: full batch (averaging single losses into
J )
Three examples: ( y = 1 , y ^ = 0.9 ) , ( y = 0 , y ^ = 0.2 ) , ( y = 1 , y ^ = 0.4 ) . Find the batch cost J .
Forecast: which of the three examples contributes the most loss?
ℓ 1 = − log ( 0.9 ) = 0.1054 (truth 1, term log y ^ ).
ℓ 2 = − log ( 1 − 0.2 ) = − log ( 0.8 ) = 0.2231 (truth 0, term log ( 1 − y ^ ) ).
ℓ 3 = − log ( 0.4 ) = 0.9163 (truth 1, but only 0.4 confidence in the true class).
Why these steps? Each example uses only the term matching its own label, as in Ex 1–3.
J = 3 1 ( 0.1054 + 0.2231 + 0.9163 ) = 3 1.2448 = 0.4149 nats.
Why this step? J is defined here as the average of the per-example ℓ values, so it doesn't grow just because N grows — see the N 1 in the parent's boxed formula.
Verify: the hesitant-but-correct example 3 dominates, as expected. And 0.4149 < 0.693 , so this model already beats the coin-flip baseline of Ex 4. ✓ Feeds straight into Gradient Descent via the ( y ^ − y ) x update.
Worked example Ex 6 — Degenerate cell:
y ^ → 0 and y ^ → 1 (limits & clipping)
What is the loss as the model becomes infinitely sure? Take y = 1 .
Forecast: is the loss ever exactly 0 ? Ever exactly ∞ ?
As y ^ → 1 − : ℓ = − log ( y ^ ) → − log ( 1 ) = 0 .
Why this step? Perfect confidence in the true class → zero loss, but only in the limit ; y ^ = 1 is never actually output by a finite-input sigmoid.
As y ^ → 0 + : ℓ = − log ( y ^ ) → + ∞ .
Why this step? log of a number heading to 0 is a huge negative → loss explodes.
Danger: plugging y ^ = 0 literally gives log 0 = − ∞ ⇒ NaN.
Why this step? Computers can't store − ∞ cleanly; the training blows up.
Fix: clip to [ ε , 1 − ε ] with ε = 1 0 − 7 . Then the worst single loss for y = 1 is − log ( 1 0 − 7 ) = 16.118 nats.
Why this step? Clipping caps the punishment at a large-but-finite number, keeping arithmetic safe.
Verify: − log ( 1 0 − 7 ) = 7 ln 10 = 16.118 . Finite → no NaN. This is the vertical wall you see the blue curve approaching at the left edge of the figure. ✓
Worked example Ex 7 — Real-world word problem
A spam filter is shown 4 emails. Truth (1 = spam): [ 1 , 0 , 1 , 0 ] . The filter's spam-probabilities: [ 0.99 , 0.02 , 0.30 , 0.85 ] . Score the filter with log-loss and say which email embarrassed it most.
Forecast: email 4 (real mail flagged 85% spam) — will it be the biggest contributor?
Email 1 (y = 1 ): − log ( 0.99 ) = 0.01005 . Confident & right.
Email 2 (y = 0 ): − log ( 1 − 0.02 ) = − log ( 0.98 ) = 0.02020 . Confident & right.
Email 3 (y = 1 ): − log ( 0.30 ) = 1.20397 . Right side but unsure → medium loss.
Email 4 (y = 0 ): − log ( 1 − 0.85 ) = − log ( 0.15 ) = 1.89712 . Confident & WRONG → biggest.
Why these steps? Spam (y = 1 ) uses log y ^ ; ham (y = 0 ) uses log ( 1 − y ^ ) .
J = 4 1 ( 0.01005 + 0.02020 + 1.20397 + 1.89712 ) = 4 3.13134 = 0.78283 nats.
Why this step? Average the four per-email losses.
Verify: email 4 contributes the most, matching the forecast; and J = 0.783 > 0.693 , so despite two great calls, the one confident false alarm drags the filter below the coin-flip baseline. That is log-loss doing its job. ✓
Worked example Ex 8 — Exam twist: units (
log 2 ) + a gradient step
(a) Redo the always-0.5 baseline in bits (base-2 log). (b) For one example with z = w ⊤ x + b , y ^ = σ ( z ) = 0.7 , true y = 1 , and feature x j = 2 , compute ∂ ℓ / ∂ w j .
Forecast: will the base-2 baseline be a rounder number than 0.693 ?
(a) ℓ = − log 2 ( 0.5 ) = − ( − 1 ) = 1 bit.
Why this step? log 2 just rescales by 1/ ln 2 ; the natural-log baseline ln 2 becomes exactly 1 bit — the "one coin-flip of information" reading.
(b) From the parent's cancellation, ∂ ℓ / ∂ z = y ^ − y = 0.7 − 1 = − 0.3 .
Why this step? The messy y ^ ( 1 − y ^ ) from the Sigmoid function cancels, leaving prediction − truth.
Chain rule: ∂ ℓ / ∂ w j = ( y ^ − y ) x j = ( − 0.3 ) ( 2 ) = − 0.6 .
Why this step? ∂ z / ∂ w j = x j because z is linear in w j .
Verify: − log 2 0.5 = 1 exactly (round, as forecast). The gradient − 0.6 is negative, meaning w j should increase to push y ^ toward the true 1 — exactly the direction Gradient Descent wants. ✓
Recall Across all cells, which single number is the "pass/fail line"?
ln 2 ≈ 0.693 nats (or 1 bit). A batch loss J below it means your model beats random guessing; above it means it's worse than a coin flip.
Recall Why did Ex 2 hurt ~58× more than Ex 1 rewarded, even though the prediction was equally "confident"?
The loss curve − log y ^ is not symmetric: near y ^ = 1 it flattens toward 0 , but near y ^ = 0 it rockets to ∞ . Confident-and-wrong is punished on the steep side. See the blue curve in the figure.