2.2.10 · D5Linear & Logistic Regression
Question bank — Log-loss - binary cross-entropy
Before you start, make sure these three symbols feel automatic:
- — the true label, a hard 0 or 1.
- — the model's predicted probability that the label is 1 (the open interval means it can get close to 0 or 1 but never touch them).
- — the loss for one example. Because is 0 or 1, only one bracket term ever survives.
True or false — justify
Log-loss can take negative values.
False. so ; the leading minus flips it to a positive number, and the floor is (only reached in the unreachable limit of perfect confidence on the true class).
Minimising log-loss is the same as maximising the Bernoulli likelihood of the data.
True. Log is monotone increasing, so it preserves the location of the maximum; negating and averaging turns "maximise likelihood" into "minimise mean negative log-likelihood", which is exactly log-loss.
For a single example, both the term and the term contribute to the loss.
False. Since is exactly 0 or 1, one term is multiplied by and vanishes; the loss is always just .
A model that always outputs has zero log-loss because it never commits.
False. Each point costs ; that is the baseline every useful model must beat, not zero.
If two models have the same classification accuracy, they must have the same log-loss.
False. Accuracy only cares which side of you land on; log-loss also punishes how confidently you are right or wrong, so a timid-but-correct model and a bold-but-correct model score differently.
Log-loss and cross-entropy are different losses that happen to look similar.
False. Binary log-loss is the cross-entropy between the one-hot true distribution and the predicted ; it is the same quantity written for the two-class case.
Swapping base from to changes which parameters minimise the loss.
False. Changing base multiplies the whole loss by a constant (), and scaling by a positive constant never moves the minimiser — only the numeric loss value changes (nats vs bits).
The gradient is a lucky coincidence special to this one example.
False. It holds for every example and stems from a deliberate cancellation between and the sigmoid derivative ; that clean form is precisely why log-loss was chosen.
Spot the error
"Log-loss uses squared error inside because we still want to penalise big mistakes."
Error: there is no square anywhere. The penalty comes from , which grows unboundedly as the true class's probability drops toward 0 — far harsher than a square, which stays finite.
"With the sigmoid, MSE is fine because is still the error we shrink."
Error: with , MSE is non-convex in the weights and its gradient carries an extra factor that vanishes when the model is confidently wrong, so learning stalls exactly when it shouldn't.
"To be safe against tiny probabilities, feed when the model is sure the label is 0."
Error: , so if the true label were actually 1 you'd get infinite/NaN loss. Always clip into instead of using exact 0 or 1.
"Since only one log term survives, the half is useless and can be deleted."
Error: it survives whenever . Both halves are needed so the single formula covers both label values; deleting one breaks every negative example.
"Log-loss is derived by choosing a formula that happens to be convex."
Error: convexity was not assumed — it falls out of maximum-likelihood on a Bernoulli model. We derived from a probability principle and got convexity as a bonus.
"The in front of changes the optimal weights, so drop it for speed."
Error: dividing by the constant rescales the loss and its gradient uniformly, leaving the minimiser unchanged; it only keeps the number scale-independent across batch sizes.
"Because the gradient is , log-loss and MSE on a sigmoid give identical updates."
Error: MSE's gradient is — it carries the extra shrinking factor. Log-loss's gradient is the clean with no such factor, which is the whole point.
Why questions
Why take the logarithm of the likelihood at all?
It turns the product over independent points into a sum (easier to differentiate) and prevents numerical underflow from multiplying many tiny probabilities; being monotone, it doesn't move the optimum.
Why does confidently wrong cost so much more than mildly wrong?
The loss is of the true class's probability; as that probability shrinks toward 0, shoots to , so screaming a wrong answer is punished far more steeply than hedging.
Why does the messy cancel in the gradient?
The chain rule multiplies by the sigmoid derivative ; the denominator and the sigmoid factor are identical and divide out, leaving .
Why do we average over instead of summing?
Averaging makes the loss (and its gradient magnitude) independent of how many points are in the batch, so a learning rate tuned on one batch size still behaves sensibly on another.
Why is a Bernoulli — not a Gaussian — the right noise model here?
The target is a binary outcome (0 or 1), which a Bernoulli describes exactly; a Gaussian assumes continuous, symmetric noise and would lead you back to MSE, which we rejected.
Why must live in the open interval rather than ?
The sigmoid asymptotes to 0 and 1 but never reaches them, and of the endpoints is ; staying strictly inside keeps the loss finite and its gradient defined.
Edge cases
What is the loss when the prediction is perfect ()?
It tends to but never exactly reaches it, since can approach 1 only in the limit; log-loss has an infimum of 0 that is approached, not attained.
What happens to the loss as a confidently-wrong prediction gets worse ()?
It diverges to ; the harsh, unbounded penalty on the true class's vanishing probability is the deliberate design feature that discourages overconfident errors.
For a completely uncertain model () does the loss depend on the true label?
No — both classes get probability , so the loss is whether or ; the baseline is label-blind.
If a class is perfectly separable, why can log-loss keep decreasing without bound during training?
The model can push ever larger, driving toward 1 on positives and 0 on negatives; the loss keeps shrinking toward 0, so weights can blow up unless regularisation or early stopping intervenes.
Does log-loss handle an imbalanced dataset (say 99% negatives) on its own?
Not automatically — every point contributes equally, so a model can score low loss by always predicting the majority class; you must add class weights or resampling if the rare class matters.
How does binary log-loss relate to its multi-class cousin?
It is the two-class special case of categorical cross-entropy; replace the sigmoid with a softmax over classes and log-loss generalises to summed over one-hot labels.
Connections
- Log-loss - binary cross-entropy — the parent note these traps stress-test.
- Sigmoid function — its derivative is what cancels in the gradient.
- Mean Squared Error — the rejected alternative several traps compare against.
- Maximum Likelihood Estimation — origin of the loss, referenced in the "why" traps.
- Cross-Entropy and KL Divergence — the identity behind the "same quantity" trap.
- Gradient Descent — consumer of the clean gradient.
- Logistic Regression — the model whose loss this is.
- Softmax and Categorical Cross-Entropy — the multi-class generalisation in the last edge case.