2.2.10 · D2Linear & Logistic Regression

Visual walkthrough — Log-loss - binary cross-entropy

1,876 words9 min readBack to topic

We will use only three colours in the figures: white for the raw geometry, cyan for the quantity we are currently building, and amber for the punchline of each step. Watch the amber.


Step 1 — A single guess is a number between 0 and 1

WHAT. A logistic model looks at an example and outputs one number, (say "y-hat"). It is a probability: the model's stated chance that the true label is .

WHY start here. Before we can score a guess, we must be crystal-clear on what a guess is. It is not a category ("cat"/"dog"); it is a slider between and . The label itself is only ever or — a hard fact. The gap between the soft and the hard is the entire story.

PICTURE. The slider below runs from (certain the answer is ) to (certain the answer is ). The cyan dot is where the model placed itself.

Figure — Log-loss  -  binary cross-entropy

Step 2 — One formula that reports the probability of the actual outcome

WHAT. We want a single expression that, given the model's slider , tells us the probability the model assigned to whatever actually happened.

WHY. If the truth is , the relevant number is . If the truth is , the relevant number is (the leftover chance the slider kept for outcome ). We do not want two formulas glued with an "if". One clean formula is:

The trick is the exponent. Anything to the power is (it disappears), anything to the power is itself (it stays). So the exponent is a switch: exactly one factor survives.

PICTURE. Two arrows. When the switch turns on the arrow; when it turns on the arrow. This is the [[Sigmoid function|]]-fed Bernoulli model.

Figure — Log-loss  -  binary cross-entropy

Step 3 — Many examples multiply (that's what independence means)

WHAT. Real data is examples, not one. Give each its own slider and label . The chance the model produces the whole dataset exactly as observed is the product of the individual chances.

WHY a product. "Independent" means one example's outcome does not lean on another's. The probability of two independent things both happening is the product (like getting heads AND heads: ). So:

Here (capital pi) just means "multiply all of these together", the way means "add them all". This is the likelihood: how believable the whole dataset is under the current model. The best model is the one making biggest — this is Maximum Likelihood.

PICTURE. A chain of per-example bricks multiplied into one tall stack. Notice how the stack's height collapses toward the moment any single brick is tiny — one confident mistake tanks the whole product.

Figure — Log-loss  -  binary cross-entropy

Step 4 — Take the log: products become sums, tiny numbers become manageable

WHAT. Apply (natural log, base ) to . Because , every product turns into a sum:

The exponents from Step 2 came down in front of the logs (that's the rule ), turning switches-in-exponents into switches-as-multipliers.

WHY log, of all tools? Three reasons, each answering "why not something else":

  1. Turns product into sum — sums are easy to differentiate term by term; products aren't.
  2. Monotone never re-orders; whatever maximizes also maximizes . We lose no information about where the best model is.
  3. Rescues tiny numbers — a product of thousands of probabilities underflows to on a computer; its log stays a comfortable finite number.

PICTURE. The tall multiplicative stack from Step 3 is flattened into a lineup of additive bars — same information, friendlier shape. Each is negative (log of a number below ), shown dipping below the axis.

Figure — Log-loss  -  binary cross-entropy

Step 5 — Flip the sign and average: the loss is born

WHAT. Optimizers like to minimize. Maximizing = minimizing . Divide by so the number doesn't grow just because you added more data:

Term by term: the is "make it a minimizable average"; the bracket is the per-example log from Step 4; the sum runs over the dataset.

WHY negate. is negative, so is a pile of negatives. The leading minus flips the whole pile positive — a loss you drive down toward . This is exactly the cross-entropy between the true label and the prediction.

PICTURE. The one-example curve for a fixed truth. When , loss is near at and shoots to as . When , it's the mirror image. Amber marks the "confident-and-wrong" cliff.

Figure — Log-loss  -  binary cross-entropy

Step 6 — Every corner case, shown explicitly

WHAT. We check all four regions of the (truth, guess) grid plus the degenerate endpoints, so no reader meets an unshown scenario.

WHY. A loss you don't trust at the edges is a loss you don't trust. Let's walk them.

  • Truth , guess high (): . Humble-and-right → cheap.
  • Truth , guess low (): . Confident-and-wrong → catastrophic.
  • Truth , guess low (): . Cheap again (mirror image).
  • Truth , guess high (): . The other cliff.
  • The fence-sitter (): cost is for every label. This is the baseline any real model must beat.
  • Degenerate endpoints ( or exactly): → NaN. Forbidden. In code, clip to .

PICTURE. The full grid of quadrants, amber on the two "confident-and-wrong" corners, cyan on the two "right" corners, and the grey waterline running through the middle.

Figure — Log-loss  -  binary cross-entropy

Step 7 — Differentiate, and watch the mess cancel

WHAT. Set and . The sigmoid has the gift . Differentiate with respect to using the chain rule:

Then multiply by :

WHY this is the payoff. The ugly in the denominator is exactly the we multiply by — they annihilate. The gradient is just prediction − truth, precisely like linear regression, but without MSE's vanishing-gradient disease. When the model is confidently wrong, stays large, so gradient descent pushes hard right when you need it most.

PICTURE. Two curves overlaid: the amber (which flatlines to when confidently wrong — MSE's trap) and the cyan (which stays a healthy size). The cancellation is drawn as the amber factor being crossed out.

Figure — Log-loss  -  binary cross-entropy
Recall Why doesn't MSE get this cancellation?

MSE's derivative carries a lone with nothing to cancel it. When is large and wrong, , so the gradient dies — learning stalls. Log-loss's produces a that eats the . That's the whole reason we switched.


The one-picture summary

Figure — Log-loss  -  binary cross-entropy

One guess → Bernoulli probability → multiply across data → log to a sum → negate and average → the log-loss surface → differentiate to . Every arrow above is a step you just walked.

Recall Feynman: the whole walkthrough in plain words

The model puts a bet on a slider from 0 to 1 for "is this a 1?". I have one honest formula that reads off how much probability the model actually gave to what really happened. For a whole dataset of independent examples I multiply all those probabilities — but products of tiny numbers vanish, so I take the log, which turns the multiply into an add and behaves nicely. Bigger is better for "how believable is the data", so to make something to minimize I flip the sign and divide by how many examples there are. The resulting curve is gentle when I'm humble-and-right and a vertical cliff when I'm confident-and-wrong — never bet the farm on a mistake. Finally, when I ask "which way should I nudge the weights?", all the messy sigmoid factors cancel and I'm left with the cleanest possible answer: guess minus truth. That's log-loss, birth to gradient.


Connections