5.6.2 · D3Machine Learning (Aerospace Applications)

Worked examples — Logistic regression — sigmoid, cross-entropy loss

2,282 words10 min readBack to topic

This is a worked-example deep dive for Logistic Regression. The parent note built the machinery — sigmoid, cross-entropy, the gradient . Here we stress-test it against every case the topic can throw at you: every sign of , the degenerate boundary, the saturated tails where gradients vanish, a real aerospace word problem, and an exam-style twist.

Nothing new is assumed. If a symbol appears, it was earned in the parent. Let us first name the cases.


The scenario matrix

Every logistic-regression computation lives in one of these cells. We will hit all of them.

Cell What makes it distinct Covered by
A. , correct model confident and right Ex 1
B. , wrong model confident and wrong — big loss Ex 2
C. boundary exactly — the decision knife-edge Ex 3
D. Saturated tail huge — sigmoid flat, gradient Ex 4
E. Multi-feature vector full with mixed signs Ex 5
F. Full descent step forward → loss → gradient → weight update Ex 6
G. Real-world word problem translate English → features → prediction Ex 7
H. Exam twist odds/log-odds reasoning, no calculator sigmoid Ex 8
Figure — Logistic regression — sigmoid, cross-entropy loss

Look at the S-curve above. The four coloured dots are the characters of this whole page: green (confident-right, cell A), red (confident-wrong, cell B), yellow (the boundary, cell C), and the faded dots way out on the tails (saturation, cell D). Keep this picture in your head.


Example 1 — Cell A: , prediction correct

Forecast: guess before computing — looks positive, , so we expect and a small loss. Write your guess down.

  1. Compute the logit . . Why this step? Everything downstream needs ; it is the single number that carries all feature information into the sigmoid.
  2. Squash. . Why this step? The sigmoid turns the raw score into a probability. , exactly as forecast.
  3. Loss. Since , only the first term survives: . Why this step? Cross-entropy with is ; a probability near 1 gives a loss near 0.

Verify: ✓ (predicts class 1, matches truth). Loss is small ✓. Sanity: , and of a number just under 1 is a small negative, so is small positive. Forecast confirmed.


Example 2 — Cell B: , prediction WRONG

Forecast: truth is cloudy () but if comes out negative the model votes "not cloudy" — a confident wrong answer, so expect a large loss.

  1. Logit. . Why this step? Mixed-sign weights: the bright pixels push up, the variance term pushes down. The net is negative.
  2. Squash. . Why this step? : the model says "probably not cloudy."
  3. Loss. , so . Why this step? Cross-entropy punishes confident wrongness — being at when the answer is 1 costs about 5× the loss of Example 1.

Verify: but → misclassified ✓. Compare losses: (wrong) vs (right) — the wrong case hurts far more, exactly what a good loss should do ✓.


Example 3 — Cell C: the boundary (degenerate)

Forecast: will be exactly , so — the model is maximally uncertain. What does the gradient look like when the model refuses to commit?

  1. Logit. . Why this step? This is the decision boundary: is where "class 0" and "class 1" tie.
  2. Squash. . Why this step? , so the sigmoid gives exactly one half — the yellow dot in the figure above.
  3. Loss. , so . Why this step? At the boundary the loss is regardless of the true label — total uncertainty costs the same either way.
  4. Gradient. . Why this step? Positive gradient → descent will decrease , pushing below 0 so drops toward the correct class 0.

Verify: exactly ✓. Loss ✓. The gradient is non-zero at the boundary — this is why logistic regression keeps learning even from ambiguous points, unlike a hard threshold.


Example 4 — Cell D: saturation, where gradients vanish

Forecast: is huge, so is glued to . When the model is right, expect a tiny gradient. When it is wrong at (a confident blunder), the gradient can only be as large as the feature allows — watch.

Figure — Logistic regression — sigmoid, cross-entropy loss
  1. Logit and squash. , . Why this step? The sigmoid is essentially flat here (see the near-horizontal curve at the right of the figure).
  2. Gradient, correct label . . Why this step? Almost zero. The model is right and confident, so there is nothing to learn — descent barely moves. This is the vanishing-gradient tail.
  3. Gradient, wrong label . . Why this step? A confident wrong prediction gives the maximum possible gradient magnitude (near 1× the feature) — the model is dragged back hard.

Verify: ✓. Correct-label gradient (tiny) ✓; wrong-label gradient (large) ✓. The gradient magnitude is bounded by 1 — saturation limits learning speed when right, but never when catastrophically wrong.


Example 5 — Cell E: multi-feature vector, mixed signs

Forecast: three features tug in different directions; add the bias. Guess the sign of before crunching.

  1. Dot product term by term. . Why this step? The dot product is a weighted vote: each feature times its weight, summed. Negative weights let a feature vote for class 0.
  2. Add bias. . Why this step? The bias shifts the whole decision boundary; here it softens the negative vote slightly.
  3. Squash. .
  4. Loss. , so . Why this step? means we want small; is reasonably small, so the loss is modest.

Verify: ✓, and truth is class 0, so the model is right → small loss ✓.


Example 6 — Cell F: one complete gradient-descent step

Forecast: is positive but not confident; , so we expect to increase after the step.

  1. Forward. ; . Why this step? Establishes the current prediction before we can measure how wrong it is.
  2. Loss. . Why this step? Records the "before" cost so we can later confirm the update helped.
  3. Gradient. . Why this step? Negative → prediction too low for ; descent must add to .
  4. Update. . Why this step? Gradient descent moves against the gradient; the double negative raises .

Verify: New , new — up from , closer to the target ✓. New loss ✓. The step reduced the loss, so it moved in the right direction.


Example 7 — Cell G: aerospace word problem

Forecast: both features are high with positive weights → high risk expected. Guess: flagged.

  1. Translate English to . . Why this step? Each standardized feature times its weight gives its contribution to risk; the bias sets the baseline threshold.
  2. Probability. . Why this step? Converts the risk score into an interpretable failure probability — .
  3. Decision. flag for inspection. Why this step? Applies the operational threshold; here safety wins.

Verify: ✓, decision consistent with "high hours + high vibration = risky" ✓. Failure probability — plug : , ✓.


Example 8 — Cell H: exam twist (odds & log-odds, no sigmoid table)

Forecast: the parent note showed . So odds , and adding to multiplies the odds. Guess: doubling-ish.

  1. Odds from log-odds. . Why this step? By definition , so exponentiating undoes the log. Odds in favour of cloudy.
  2. Probability from odds. . Why this step? Rearranging gives — this equals , but we never needed a sigmoid table.
  3. Effect of on odds. New odds . Why this step? This is the whole point of log-odds being additive: a fixed bump in multiplies the odds by a constant factor , no matter where you started.

Verify: ✓ — matches step 2 exactly. The odds-multiplier ✓. This is the odds interpretation that makes coefficients readable: weight means "each unit of multiplies the odds by ."


Recall Quick self-test across all cells

Which cell has exactly? ::: Cell C, the boundary . When is the gradient vanishingly small? ::: Cell D — saturated tail AND already correct. What does adding to do to the odds? ::: Multiplies them by (Cell H). Why does a confident wrong prediction still learn fast? ::: Its gradient magnitude is near 1, the maximum. The one-line gradient formula? ::: averaged over examples.