Exercises — Loss functions - MSE, cross-entropy
Before we start, one reminder of the two toolboxes you will reach for, so no symbol is unearned:
Level 1 — Recognition
L1.1 — Name the loss
For each task, name whether you would reach for MSE or Cross-Entropy, and say which noise model justifies it. (a) Predicting tomorrow's temperature in °C. (b) Deciding if an email is spam (yes/no). (c) Sorting a photo into {cat, dog, bird}.
Recall Solution L1.1
(a) MSE — the target is a real number; we assume Gaussian noise around the prediction. (b) Cross-Entropy (binary) — the label is a coin flip, so a Bernoulli model fits. (c) Cross-Entropy (categorical) — one of classes, so a Categorical model fits. The rule: real numbers ⇒ Gaussian ⇒ MSE; categories ⇒ Bernoulli/Categorical ⇒ CE.
L1.2 — Read the formula
In , what does each of and mean, and what range must live in?
Recall Solution L1.2
- is the true label, exactly or .
- is the model's predicted probability that .
- must be in the open interval — never exactly or , because . This is why we pass raw scores through a sigmoid first.
Level 2 — Application
L2.1 — Compute an MSE
True prices . Model guesses . Compute the MSE.
Recall Solution L2.1
WHAT: subtract, square, average. Errors . Squares . Why square? To make every error positive and to punish the big miss (the ) far more than a small one.
L2.2 — Compute a BCE
True label . Model says . Compute the BCE.
Recall Solution L2.2
Since , the term vanishes: A confident-and-correct guess ⇒ small loss. Good.
L2.3 — Softmax then CE
Logits , true class (the middle one). Find the predicted probabilities and the cross-entropy.
Recall Solution L2.3
Step 1 — exponentiate (softmax numerator turns any real number into a positive weight): . Step 2 — normalize (divide by the sum so weights become probabilities that add to 1): Sum . Probs . Step 3 — pick the true class (one-hot kills the other two terms):
Level 3 — Analysis
L3.1 — The coin-flip baseline
On a binary task the model outputs no matter the input (pure guessing). Show the BCE equals the same value whether the truth is or , and identify that value.
Recall Solution L3.1
If : . If : . Either way: Meaning: is the "learned nothing" line. If training loss sits near , the model is no better than a coin flip. It is your first sanity-check baseline.
L3.2 — Why CE punishes confident wrongness
Compare the BCE for a correct confident guess vs a wrong confident guess, both at probability toward the model's chosen class. Truth is .
Recall Solution L3.2
Confident and right: ⇒ . Confident and wrong: the model is 99% sure of class 0, i.e. ⇒ . Look at figure s01: near the curve hugs the floor, but as it rockets to infinity.

Why this shape matters: blows up near , so a confident wrong answer earns a savage penalty ( the confident-right loss here). That steep wall is exactly what forces the network to correct bold mistakes fast — a fairness the flatter MSE curve does not provide.
L3.3 — MSE vs CE gradient when confidently wrong
A sigmoid unit outputs . Truth , but the unit is confidently wrong: . (a) The CE gradient w.r.t. the logit is . Evaluate it. (b) The MSE-on-sigmoid gradient w.r.t. the logit is . Evaluate it. Which loss still "pushes"?
Recall Solution L3.3
(a) CE: . A strong push toward the truth. (b) MSE: . A tiny push — about weaker. The extra factor shrinks to exactly when the sigmoid saturates (near or ). So MSE-on-sigmoid stalls precisely when the model is boldly wrong — the worst time to stall. See Backpropagation for how this factor enters the chain rule, and Gradient Descent for why a vanishing gradient freezes learning.
Level 4 — Synthesis
L4.1 — Derive BCE as the case of categorical CE
Categorical CE is . Show that with two classes it becomes the binary formula .
Recall Solution L4.1
WHAT: write the two-class sum explicitly. Let class 1 be "positive" with probability , and class 0 be "negative" with probability (they must sum to 1). Let the one-hot truth be , . WHY it matters: BCE is not a separate law — it is categorical CE with the redundant second probability folded in via . One idea, two dresses.
L4.2 — From Gaussian to MSE
Starting from the one-point Gaussian negative log-likelihood explain, term by term, why minimizing this over is identical to minimizing .
Recall Solution L4.2
- The first term contains no — it is a constant as far as our knob is concerned, so it cannot move the location of the minimum. Drop it.
- The second term is . The factor is a positive constant multiplier; scaling a function by a positive number does not move where its minimum sits.
- What remains that actually depends on is . Conclusion: . Averaging over points gives MSE. This is why MSE is Gaussian maximum likelihood in disguise.
L4.3 — Total loss with regularization
A model has MSE on its data. We add an L2 penalty with and weights . Compute the total objective the optimizer actually minimizes.
Recall Solution L4.3
. Penalty . Why add it: the data-loss alone can be minimized by wild, large weights that overfit; the penalty makes big weights cost something, trading a little training fit for better generalization.
Level 5 — Mastery
L5.1 — Prove the softmax+CE gradient is
For one example, with softmax and one-hot . Show .
Recall Solution L5.1
Step 1 — softmax derivative. Differentiating the ratio gives the standard identity where if else . (This is just the quotient rule applied to .) Step 2 — chain rule into CE. Step 3 — split the sum. using (one-hot sums to one) and . WHY it matters: the messy softmax Jacobian collapses to prediction − truth, the same beautiful form MSE-with-identity-output has. Clean gradients, no vanishing when confidently wrong.
L5.2 — Numeric confirmation of s01's claim
Confirm numerically that for truth , moving from down to multiplies the BCE by more than , illustrating the steep wall.
Recall Solution L5.2
. . Ratio . ✓ The loss doesn't grow linearly as you get worse — it grows ferociously near the wrong end, which is the whole point of .
L5.3 — Design decision under a twist
You must predict a probability-like quantity (a real number in , e.g. "fraction of pixels that are foreground"), not a class label. Which loss, and why is this subtle?
Recall Solution L5.3
This is a regression target that merely lives in ; it is a continuous quantity, not a Bernoulli outcome. The clean choice is MSE (or a bounded regression loss), because the noise model is roughly Gaussian on the fraction, not a coin flip. The subtlety: BCE is only justified when is genuinely and is a probability of that event. Using BCE with fractional (so-called "soft labels") is sometimes done deliberately, but it silently changes the assumed model. Rule of thumb: ask "is my target a bet outcome (use CE) or a measured amount (use MSE)?" — the range alone does not decide it. Compare with Logistic Regression, where the target really is and BCE is exactly right.
Recall Full self-test checklist
You have mastered this page if you can, from a blank sheet:
- compute MSE, BCE, and softmax-CE from raw inputs (L2),
- state and use the coin-flip baseline (L3),
- explain the vanishing-gradient stall of MSE-on-sigmoid (L3),
- derive BCE from categorical CE and MSE from the Gaussian (L4),
- prove the softmax+CE gradient equals (L5).