Visual walkthrough — Logistic regression — sigmoid, cross-entropy loss
This page rebuilds the entire engine of logistic regression as a sequence of pictures. We start from a single yes/no question and end at the one gradient formula that trains everything. Every symbol is drawn before it is used.
Step 1 — A question with only two answers
WHAT. Imagine one turbine blade. We ask a single question: will it fail? There are only two answers: yes (we write this as the number ) and no (we write ).
WHY. Everything downstream needs a target to aim at. Using the numbers and (instead of the words "yes"/"no") lets us do arithmetic on the answer later — subtract it, multiply it, average it.
PICTURE. Two dots on a vertical axis: a red dot at height ("fail"), a black dot at height ("safe"). There is nothing in between — a real blade either failed or it didn't.
Step 2 — The raw score lives on the whole number line
WHAT. Our model looks at a feature (say, operating hours) and computes one plain number:
WHY. This is the only thing a machine finds cheap: multiply and add. But notice the disaster — can be anything: , , . It is not a probability. A probability must sit between and . We have a mismatch to fix.
PICTURE. A horizontal number line stretching from to with sliding freely along it. The red segment marks the only region a probability is allowed to live in — a tiny sliver from to — showing how badly overshoots.
Step 3 — Why odds is the natural bridge
WHAT. We connect the free number to a probability (the chance the answer is ) using odds: and we assume the model outputs the log of the odds:
WHY. Why odds and not itself? A probability is trapped in , but odds run from (certain "no") up to (certain "yes"). Take the logarithm and the range explodes both ways: to — exactly the range already lives in. So log-odds is the one transformation that makes "unbounded number" and "probability" speak the same language. Choosing (not some other stretch) is what makes feature effects add up linearly.
PICTURE. Three stacked bars showing the same belief in three languages: probability (a bar capped at 1), odds (a taller bar), log-odds (a signed value on a two-way axis). The red arrows show each transform stretching the range wider.
Step 4 — Solve for : the sigmoid is forced into existence
WHAT. We now undo the log-odds to get back. Starting from (exponentiate both sides — cancels ): Multiply top and bottom by :
WHY. We didn't invent this S-curve for fun — it is the only function consistent with "log-odds are linear". Term by term: shrinks to as (so ), and blows up as (so ). The in the denominator guarantees we never divide by zero and never exceed .
PICTURE. The famous S: a smooth red curve from height (far left) to height (far right), passing dead-center through . The flat tails and the steep middle are labelled.
All cases covered.
- — the fence-sitting decision boundary.
- — total confidence in "yes".
- — total confidence in "no".
Step 5 — The slope of the S is a hidden gift
WHAT. How fast does the S climb? Differentiating (chain rule on ):
WHY. We need the slope because gradient descent moves downhill, and "downhill" means "in the direction of the slope". The magic is that the slope is expressed purely in terms of the output itself — no leftover exponentials. That's why training is cheap: once you know the probability, you instantly know the slope.
PICTURE. The S-curve again in black, with its slope curve drawn in red beneath — a bell-shaped hump peaking at (value ) and vanishing at both tails, showing where the curve is steep vs. flat.
Step 6 — Scoring surprise: cross-entropy
WHAT. We now need a number that says "how bad was this guess against the truth ?" The score is
WHY. Look at how the two labels switch it on and off. Term by term:
- If : the term vanishes, leaving .
- If : the term vanishes, leaving .
Only the term matching the true answer survives. And is exactly "surprise": if you gave the truth probability , surprise is ; if you gave it probability near , surprise rockets to . Confident wrong answers are punished mercilessly.
PICTURE. Two curves: red is (the cost when ) diving toward as and shooting up as ; black is (the cost when ), its mirror image.
Step 7 — Why this exact score? Maximum likelihood
WHAT. Assume each label came from a coin biased with probability (a Bernoulli trial): The chance of seeing all the data (independent) is the product; take its log and negate:
WHY. We didn't pick cross-entropy by taste — it is the negative log-likelihood. Maximising likelihood = "choose weights that make the data we actually saw the least surprising". The turns an unwieldy product into a friendly sum (sums differentiate term-by-term); the minus turns "maximise" into "minimise", so we can hand it to gradient descent. This is the maximum-likelihood view.
PICTURE. A row of Bernoulli coins, each with its own bias , multiplied into one long product (red), then a downward arrow to the same thing rewritten as a sum of logs — visually shrinking a scary product into a tidy sum.
Step 8 — The miracle cancellation: the gradient
WHAT. We want the slope of the loss with respect to a weight . Chain three pieces: Multiply the first two: the ugly fractions get cancelled by the from Step 5: So
WHY. This is why we chose sigmoid and cross-entropy together: the sigmoid's slope (which caused saturation in Step 5) is exactly the thing that appears in the numerator of — and it cancels. The freeze is cured. What remains is pure meaning: error times feature . Wrong-and-confident? Big error, big correction.
PICTURE. A three-box chain diagram: box , box , box feeding an arrow; the two middle terms shown crossing out in red, leaving at the exit.
Degenerate cases.
- Perfect prediction : gradient — nothing to fix, learning stops correctly.
- or exactly: blows up numerically → clamp into in code (and see Regularization (L1, L2) to stop weights racing to ).
The one-picture summary
WHAT. One figure that threads all eight steps: feature → linear score → sigmoid squashes into → compare to truth → cross-entropy surprise → slope collapses to → nudge .
Recall Feynman retelling — say it like you're explaining to a friend
A machine can only add and multiply, so first it makes one plain number . That number can be anything, but we wanted a probability, which has to sit between 0 and 1. The trick: assume is the log of the odds, because log-odds is the one thing that ranges over the whole number line just like does. Undo that assumption algebraically and out pops the S-shaped sigmoid — we didn't invent it, it was forced. The sigmoid's slope turns out to be , written using only its own output. Then we need a report card: cross-entropy, which is just "how surprised am I to see the true label?" and it happens to be the negative log-likelihood of a coin-flip model, so it's the mathematically honest choice. Finally, when we ask how the report card changes as we tweak a weight, the sigmoid's slope cancels the messy fractions, and everything melts down to error times feature, . That simplicity is the whole reason logistic regression is fast and stable.
Related destinations: Softmax Regression (many classes), Neural Networks (stack these units), ROC Curves and AUC (judge the classifier), Bayesian Interpretation (priors on ).