5.6.2 · D4Machine Learning (Aerospace Applications)

Exercises — Logistic regression — sigmoid, cross-entropy loss

2,312 words11 min readBack to topic

This page is a self-test ladder for the parent topic. Each problem builds on the last. Try each one before opening its solution. Every numeral you see below has been machine-checked.

Before we start, one shared picture. Everything on this page lives on the sigmoid curve: a smooth "S" that takes any real number (the logit, the raw score ) on the horizontal axis and returns a probability between 0 and 1 on the vertical axis.

Figure — Logistic regression — sigmoid, cross-entropy loss

Keep this figure in your head: left of centre → probability below 0.5 → "class 0"; right of centre → above 0.5 → "class 1"; dead centre () → exactly 0.5.


Level 1 — Recognition

Recall

Solution 1.1 What we do: plug into . What it looks like: the exact middle of the S-curve — the point marked in the figure above where the curve crosses the dotted 0.5 line. Decision: , so we predict class 1 (the boundary is inclusive by our convention).

Recall

Solution 1.2

  • = the number of training examples we average over. Averaging (not just summing) means the loss size does not blow up just because we collected more data.
  • = the true label of example , which is either or (never in between).
  • = the model's predicted probability that example is class 1, a number in .
  • The minus sign flips "log-likelihood we want to maximise" into a "loss we want to minimise". Optimisers by convention go downhill, so we hand them something that gets smaller as the fit improves.
Recall

Fill-in check as ::: as ::: The value of where equals exactly is :::


Level 2 — Application

Recall

Solution 2.1 Step 1 — logit (the dot product plus bias). Why the dot product? Because is the single number that says "how much do the features, weighted by importance, push toward class 1." Step 2 — squash it. What it looks like: sits to the left of centre in the sigmoid figure, so we land below 0.5. Decision: class 0.

Recall

Solution 2.2 What we do: use . With the second term dies (), leaving only . Why so large? Truth was class 1 but the model only gave it probability — a confident wrong lean. Cross-entropy punishes that. (Logarithms here are natural logs, base , matching the inside the sigmoid.)

Recall

Solution 2.3 Step 1 — the error term. . Negative because the prediction was too low. Step 2 — per-weight gradients. Step 3 — descend (): What it looks like: both weights moved so as to raise the next prediction — exactly what we want since the truth is class 1. This is one step of Gradient Descent.


Level 3 — Analysis

Recall

Solution 3.1 Compute: for every feature . Why it matters: the update leaves the weight unchanged. A perfectly-classified point contributes nothing to further learning — the algorithm has already "used up" what that point can teach. This is the geometric meaning of a minimum: no slope, no motion. The subtle bit: requires , which is unreachable with finite weights. So in practice the gradient shrinks toward zero but never truly hits it — the model keeps inching weights larger. That endless drift is exactly the problem Regularization (L1, L2) exists to stop.

Recall

Solution 3.2 Losses: Error terms: Reading it: the confidently-wrong case (b) has both a far larger loss and a larger-magnitude error term, so it produces a stronger corrective push. This is the whole point of cross-entropy over squared error: being confident and wrong hurts a lot, which drags the model out of that bad state fast. A learner that is merely unsure gets a gentle nudge.


Level 4 — Synthesis

Recall

Solution 4.1 Forward pass, point 1 (): Forward pass, point 2 (): Error terms: ; . Averaged gradients (): Update: What it looks like / sanity: point 1 (a non-failure the model over-predicted) pushes down; point 2 (a failure it slightly under-predicted) pushes up. They nearly cancel on , but both agree on lowering — shifting the whole S-curve rightward so scores of read as "less likely to fail." A coherent, honest step.

Recall

Solution 4.2 Key link: . So the boundary is wherever the logit is exactly zero — this is why the boundary of logistic regression is always a straight line (or hyperplane), inherited straight from Linear Regression. Set : Description: a straight line of slope and intercept . On one side (predict class 1), on the other (predict class 0). The sigmoid only decides how sharply the probability turns as you cross that line, not where the line is.


Level 5 — Mastery

Recall

Solution 5.1 Start from the definition and substitute : Multiply top and bottom by : Interpretation: the probability of "not class 1" at score equals the probability of "class 1" at the flipped score . The curve has point symmetry about . This is exactly why a two-class problem needs only one sigmoid: modelling automatically pins down . When you have more than two classes you lose this freebie and must move to Softmax Regression.

Recall

Solution 5.2 First derivative — chain the two known facts and (with ): Second derivative — differentiate ; the constant dies and : Sign: , so both factors are strictly positive, so the second derivative is strictly positive everywhere. Conclusion: a function with everywhere-positive second derivative is convex — it curves upward like a bowl, guaranteeing a single global minimum with no false valleys to trap Gradient Descent. This is the rigorous reason we abandoned squared error, and it is the payoff promised by the Maximum Likelihood Estimation derivation in the parent note.

Figure — Logistic regression — sigmoid, cross-entropy loss
Recall

Solution 5.3 . First derivative: (negative: at the loss still decreases as we push up toward the correct class 1 — good). Second derivative: (positive: the bowl curves up, confirming convexity at this point).

Recall

Mnemonic for the whole page The one identity that unlocks every exercise here ::: the gradient collapses to — "prediction minus truth, scaled by the feature."


Where to go next