2.2.10 · D4Linear & Logistic Regression

Exercises — Log-loss - binary cross-entropy

2,846 words13 min readBack to topic

Level 1 — Recognition

These test whether you can read the formula and pick the surviving term. No calculus yet.


Exercise 1.1 — Which term survives?

A single training point has true label and the model predicts . Write out , cross off the dead term, and compute the number.

Recall Solution

WHAT we do: plug into . WHY: makes the coefficient , so the second term dies. Only remains — this is the parent's "" in action. Small-ish loss because the model leaned the right way (0.7 > 0.5) but wasn't fully confident.


Exercise 1.2 — The other label

Same model output , but now the true label is . Compute .

Recall Solution

WHAT: now , so the first term dies and survives. WHY: the surviving factor is the probability the model gave to the actual class, which is class 0, whose probability is . Bigger than Exercise 1.1: the model bet against the truth here.


Exercise 1.3 — Baseline recall

A lazy model always outputs . What loss does it incur on any point, regardless of the label? Give the number and its name.

Recall Solution

WHAT: whichever term survives, the surviving probability is . WHY: if we get ; if we get . Same either way. This is the baseline the parent note names: any useful model must score below nats on average.



Level 2 — Application

Now compute full-batch losses and use the gradient .


Exercise 2.1 — Three-point batch

Compute the mean log-loss over these three points:

1 0.9
0 0.2
1 0.4
Recall Solution

WHAT: compute each (only the label-matching term), then average. WHY average: makes the number independent of batch size (parent formula). The third point (right class but only 0.4 confidence, i.e. leaning wrong) dominates the loss.


Exercise 2.2 — Single-example gradient w.r.t.

For one point with and pre-activation giving , what is ? Interpret its sign.

Recall Solution

WHAT: use the parent's cancellation result . WHY this tool: we differentiate w.r.t. (not ) because is what gradient descent actually pushes. The messy from the Sigmoid function cancels, leaving the clean error term. Interpret: negative gradient on means gradient descent will increase (step is , and subtracting a negative adds), pushing up toward — correct, since the true label is 1.


Exercise 2.3 — Weight gradient with a feature

One point, one feature: , true , current . Compute and .

Recall Solution

WHAT: apply and . WHY: chain rule multiplies the -gradient by and by . Both positive → descent decreases and , lowering and pulling down toward . Correct, since .



Level 3 — Analysis

Reason about shapes, limits, and comparisons.


Exercise 3.1 — Limiting behaviour

As a confidently-wrong prediction gets worse, while the true label is . What does do, and why is this the "whole point" of log-loss? Contrast with MSE in the same limit.

Recall Solution

WHAT: the surviving term is . As , , so WHY it matters: infinite penalty for being certain and wrong — the model can never "give up" on the truth. See figure below (red curve blows up on the left). Contrast MSE: in the limit. MSE caps the punishment at a finite , so a confidently-wrong point produces only a mild signal. Combined with the vanishing factor from the parent note, that's why MSE + sigmoid stalls. See Mean Squared Error.

Figure — Log-loss  -  binary cross-entropy

Exercise 3.2 — Symmetry check

Show that log-loss is symmetric under simultaneously flipping and . Why does this match intuition?

Recall Solution

WHAT: substitute and into . WHY intuition matches: relabelling "class 1" as "class 0" (and swapping the predicted probability to match) is just renaming the coin's faces. The loss shouldn't care what we call the classes — only how much probability landed on the true one.


Exercise 3.3 — Reading a loss curve

A point has true label . Sketch/describe for : value at , at , and the trend. Is it convex in ?

Recall Solution

WHAT / values:

  • : (perfect, unreachable in practice).
  • : (baseline).
  • : . Trend: strictly decreasing as climbs toward the truth . Convexity: for all , so yes, convex in . (Convexity in the weights additionally relies on being linear — see the Logistic Regression note.) The blue curve in the figure above shows this bowl-shaped-on-a-log-axis decay.


Level 4 — Synthesis

Combine the derivation, MLE origin, and cross-entropy view.


Exercise 4.1 — From likelihood to loss, end to end

Two independent points: and . (a) Write the Bernoulli likelihood and compute it. (b) Take and confirm it equals the mean log-loss.

Recall Solution

(a) WHAT / likelihood: independence ⇒ multiply single-point Bernoulli probabilities (parent's Maximum Likelihood Estimation chain): (b) WHAT / negative-log-average: Confirm via mean log-loss: They match because — the log turns the product into the sum, exactly the parent derivation.


Exercise 4.2 — Cross-entropy interpretation

For a single point the true distribution over classes is a spike: with all mass on the true label. Show that cross-entropy between and the model's reduces to the single-example log-loss.

Recall Solution

WHAT: take true label , so , and . For , gives . WHY this is exactly : both cases collapse to — identical to the single-example log-loss. This is precisely why the loss is called binary cross-entropy; see Cross-Entropy and KL Divergence.


Exercise 4.3 — Two-class softmax collapses to sigmoid log-loss

Softmax with two logits gives . Show this equals , so categorical cross-entropy over 2 classes is binary log-loss.

Recall Solution

WHAT: divide numerator and denominator by : WHY it matters: the softmax over 2 classes depends only on the logit difference , which behaves exactly like a single sigmoid input. Categorical cross-entropy then reduces to the binary log-loss we've been using — binary is the special case of Softmax and Categorical Cross-Entropy.



Level 5 — Mastery

One long problem that forces every idea together, plus a numerical-stability sting.


Exercise 5.1 — One full gradient-descent step, by hand

Setup: one feature, weights , bias . Two points: and . Learning rate .

(a) Compute for both points. (b) Compute . (c) Compute and . (d) Take one gradient-descent step and give the new . (e) State (without recomputing) whether the loss should drop, and why.

Recall Solution

(a) Forward pass. With , for both, so (A fresh model is maximally unsure — the baseline state.)

(b) Loss. Both surviving terms are : Exactly the always-0.5 baseline — expected, since the model is outputting 0.5.

(c) Gradients. Error terms: ; .

(d) Update. :

(e) Should the loss drop? Yes. The step raised to . Now so (pushed toward its target ), and so (pushed toward its target ). Both predictions moved the right way, and because log-loss is convex here, this step decreases . The figure traces this move.

Figure — Log-loss  -  binary cross-entropy

Exercise 5.2 — The numerical-stability sting

A student computes loss for a confidently-correct point , (the model's float rounded up). (a) What does evaluate to on a computer, and why is that a disaster? (b) Apply clipping with and give the loss.

Recall Solution

(a) WHAT breaks: is fine here, but the danger is the mirror case — , gives NaN once it enters a sum. Even at , if the other label were involved you'd hit . Any exact 0 or 1 poisons the batch with NaN, which then propagates through every gradient (parent's second [!mistake]). (b) Clipping. Replace with . For , clip to : A tiny finite loss instead of a landmine. (For , clipped to : — huge but finite, exactly the harsh-but-survivable punishment we want.)



Connections