3.1.8Neural Network Fundamentals

Loss functions - MSE, cross-entropy

1,843 words8 min readdifficulty · medium1 backlinks

1. Where do loss functions come from? (Maximum Likelihood)

WHY this matters: MSE and cross-entropy are not two random formulas to memorize — they are the same recipe (NLL) applied to two different noise models (Gaussian vs. Categorical).


2. Mean Squared Error (MSE) — for regression


3. Cross-Entropy — for classification

Figure — Loss functions -  MSE, cross-entropy

4. Forecast-then-Verify


5. Common mistakes (Steel-manned)


6. Active Recall — #flashcards/ai-ml

Every standard loss is the negative log-likelihood of what?
The data under an assumed noise/probability model.
MSE arises from assuming what noise distribution on targets?
Gaussian (normal) noise, εN(0,σ2)\varepsilon\sim\mathcal N(0,\sigma^2).
Write the MSE formula.
1ni(yiy^i)2\frac{1}{n}\sum_i (y_i-\hat y_i)^2.
Why do we square the error in MSE?
To make it positive and to penalize large errors more heavily.
Gradient of 12(yy^)2\tfrac12(y-\hat y)^2 w.r.t. y^\hat y?
y^y\hat y - y (the error itself).
Cross-entropy arises from assuming what distribution?
Bernoulli (binary) / Categorical (multi-class).
Write binary cross-entropy.
[ylogy^+(1y)log(1y^)]-[y\log\hat y+(1-y)\log(1-\hat y)].
Write multi-class cross-entropy with one-hot yy.
kyklogy^k=logy^true-\sum_k y_k\log\hat y_k = -\log\hat y_{\text{true}}.
Gradient of softmax+CE w.r.t. logit zkz_k?
y^kyk\hat y_k - y_k.
Why not use MSE for classification?
Sigmoid+MSE gradient saturates (vanishes) when confidently wrong; CE keeps gradient y^y\hat y-y.
BCE when y^=0.5\hat y=0.5 and y=1y=1?
ln20.693\ln 2 \approx 0.693 (the coin-flip baseline).
What must the input to CE's log be?
A valid probability in (0,1)(0,1), so apply softmax/sigmoid first.

Recall Feynman: explain to a 12-year-old

Imagine a dartboard. The loss is how far your dart landed from the bullseye. For guessing a number (like temperature), we measure the distance and square it — landing twice as far is four times as bad (that's MSE). For guessing a category (cat vs dog), we don't measure distance; we ask "how surprised are you by the right answer?" If you said "99% dog" and it was a dog, tiny surprise → tiny loss. If you said "1% dog" and it was a dog, huge surprise → huge loss (that's cross-entropy). Training just means nudging your throws to make the loss shrink.

Connections

Concept Map

needs scalar score

minimized by

take neg log

is general recipe for

Gaussian noise model

Categorical noise model

gradient equals

used for

used for

drives

Neural network prediction

Loss function L

Gradient descent

Maximum Likelihood

Negative log-likelihood

MSE for regression

Cross-entropy for classification

Error y-hat minus y

Real-valued targets

Class probabilities

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, loss function ka matlab simple hai: model ne prediction ki, aur hum ek single number nikaalte hain jo batata hai "kitna galat ho". Training ka pura kaam yahi hai — is number ko minimize karna, kyunki gradient descent ko differentiate karne ke liye ek scalar chahiye. Bina loss ke koi gradient nahi, koi learning nahi.

Ab do main flavours hain. MSE use hoti hai jab output ek real number ho (jaise price, temperature). Iski root maximum likelihood hai — hum maan lete hain ki target me Gaussian noise hai, aur us assumption ka negative log-likelihood nikaalo to bilkul (yy^)2(y-\hat y)^2 nikal aata hai. Square isliye lagate hain taaki error positive rahe aur bade errors ko zyada punish kare. Iska gradient super clean hai: y^y\hat y - y, yaani seedha error hi correction ban jaata hai.

Cross-entropy use hoti hai classification me, jaha output ek probability hota hai (softmax ya sigmoid se). Yaha noise model Bernoulli/Categorical hai. Idea: "sahi answer par tum kitne surprised the?" — surprise =logp= -\log p. Agar tumne sahi class ko 0.99 probability di, loss almost zero; agar 0.01 di (confidently galat), to log0.01=2.3-\log 0.01 = 2.3, bahut bada loss. Yehi asymmetry network ko fast seekhne par majboor karti hai.

Ek important baat: classification me MSE mat use karo. Sigmoid + MSE ka gradient y^(1y^)\hat y(1-\hat y) factor ki wajah se saturate ho jaata hai (zero ho jaata hai) jab model confidently galat ho — learning ruk jaati hai. Cross-entropy woh factor cancel kar deti hai, gradient wahi magic y^y\hat y - y reh jaata hai. Yaad rakhne ka trick: real numbers → Gaussian → MSE; categories → coins → Cross-Entropy. Dono ka gradient end me same beautiful form deta hai.

Go deeper — visual, from zero

Test yourself — Neural Network Fundamentals

Connections