Loss Functions and the Learning Objective

P6.nn-foundations.04 · Audience: guest, language-pro, it-ml · Prerequisites: The Feedforward Network

Real LLM grading for this pageLLM grading (this page):

The forward pass ends with a prediction — but training begins with a question: how wrong was it? A loss function compresses the answer into one scalar, the single number that measures how wrong a network's prediction is — and the starting point for every weight update. This module works through the two loss functions that dominate deep learning, MSE and cross-entropy, with concrete numbers, and shows how the loss becomes the learning signal that drives backpropagation.

Step 1 / 4What Is Loss?

In module 03 you traced a feedforward network from input x through ReLU to output ŷ. But training requires answering: how wrong is ŷ?

A loss function L(ŷ, y*) takes two inputs — ŷ, the network's predicted output, and y*, the true target (the correct answer from the training data) — and returns a single positive number. The smaller that number, the better the prediction.

Loss is the compass of training:

  • Loss = 0 → perfect prediction
  • Loss > 0 → the network is wrong by some amount
  • The gradient of the loss tells the network which direction to adjust its weights
🗣️ From a linguist's perspective: Loss as a scoring rubric applied to the network's output
A language teacher scoring a translation uses a rubric: award full marks when the translation is perfect; deduct points for each error proportional to its severity. The loss function is the machine-learning equivalent: it takes the model's output (the translation attempt) and the reference (the gold-standard translation) and returns a single number measuring how far apart they are. Just as a lower rubric score means a better translation, a lower loss means the network's prediction is closer to the correct answer. Training adjusts the network's weights to minimise this score — the same motivation behind iterative revision toward the reference in translation quality assessment.
ⓘ Concept: Loss Function

A loss function L(ŷ, y*) maps a predicted output ŷ and a true target y* to a non-negative scalar measuring prediction error:

  • L ≥ 0
  • L = 0 iff ŷ = y* (perfect prediction)
  • The gradient ∂L/∂W tells backpropagation how to adjust each weight W

Two loss functions dominate deep learning:

  • MSE (Mean Squared Error) — for regression tasks (predicting a continuous value)
  • Cross-Entropy — for classification tasks (predicting a category)

Why it matters — Transformers use cross-entropy loss because their output is a probability distribution over the vocabulary — a classification problem with tens of thousands of classes. MSE appears in regression variants and is the simpler starting point for building intuition.

🔬 Interview deep-dive — entropy, cross-entropy, KL & mutual information

Entropy H(p)=xp(x)logp(x)H(p) = -\sum_x p(x)\log p(x) is the average uncertainty of a distribution pp — equivalently, the minimum expected number of bits (with log2\log_2) needed to encode samples drawn from pp. It is maximised by the uniform distribution (every outcome equally likely = maximal surprise) and is 00 when pp puts all mass on one outcome.

Cross-entropy H(p,q)=xp(x)logq(x)H(p, q) = -\sum_x p(x)\log q(x) is the expected number of bits to encode data drawn from pp using a code optimised for qq. This is the standard classification loss: pp is the one-hot label vector and qq is the model's predicted probability distribution, so H(p,q)=logq(k)H(p, q) = -\log q(k^*) collapses to the logpk-\log p_{k^*} seen above.

KL divergence measures the extra bits incurred by coding with qq instead of pp:

DKL(pq)=xp(x)logp(x)q(x)=H(p,q)H(p)0.D_{\mathrm{KL}}(p\,\|\,q) = \sum_x p(x)\log\frac{p(x)}{q(x)} = H(p, q) - H(p) \ge 0.

It is 0\ge 0 (Gibbs' inequality), zero iff p=qp = q, and asymmetricDKL(pq)DKL(qp)D_{\mathrm{KL}}(p\|q) \neq D_{\mathrm{KL}}(q\|p) in general — so it is a divergence, not a true distance.

Key ML link. Writing p^\hat{p} for the empirical data distribution and pθp_\theta for the model, minimising cross-entropy loss equals minimising DKL(p^pθ)D_{\mathrm{KL}}(\hat{p}\,\|\,p_\theta), because H(p^,pθ)=H(p^)+DKL(p^pθ)H(\hat{p}, p_\theta) = H(\hat{p}) + D_{\mathrm{KL}}(\hat{p}\,\|\,p_\theta) and the entropy H(p^)H(\hat{p}) is constant in θ\theta. Both are therefore identical to maximum-likelihood estimation of the model parameters.

Mutual information quantifies how much knowing YY reduces uncertainty about XX:

I(X;Y)=DKL(p(x,y)p(x)p(y))=H(X)H(XY).I(X; Y) = D_{\mathrm{KL}}\big(p(x, y)\,\|\,p(x)\,p(y)\big) = H(X) - H(X\mid Y).

It is the KL divergence between the joint distribution and the product of the marginals, is symmetric, and is zero iff XX and YY are independent (the joint factorises).

Check your understanding

If a model predicts 0.9 for class 1 but the true label is 0, in what direction should the loss push the model's output?

Given predictions = [0.9, 0.2] and targets = [1.0, 0.0], what is the MSE? (round to 4 d.p. — recall MSE = (1/n) Σ (ŷᵢ − yᵢ*)²)

Given predicted probability p = 0.8 and true label y = 1, what is the cross-entropy loss −ln(p)? (round to 4 d.p.)

If the loss increases when weight w increases, should w be increased or decreased on the next gradient step?

Module review — why is cross-entropy preferred over MSE for classification tasks with softmax outputs?

📚 Go Further

Curated resources to explore loss functions and the learning objective further.

TypeResource
VideoStatQuest with Josh Starmer — Cross Entropy, Clearly Explained! and Mean Squared Error (YouTube)
Video3Blue1Brown — What is backpropagation really doing? (Deep Learning, Chapter 3, YouTube)
CourseCoursera — Deep Learning Specialization, Course 1 Week 2: Logistic Regression as a Neural Network (free audit)
ReferenceGoodfellow, Bengio & Courville, Deep Learning Ch. 6.2: Output Units and Cost Functions (free at deeplearningbook.org)

Practice

Close the module by putting the learning loop into your own words — loss, gradient, and weight update, end to end.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send
Reflection — explain how a neural network learns

Loading exercise…

Try it yourself

A scratch console for this page's ideas — ungraded, nothing you run here is recorded.

Scratch console

A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.

Output appears here.

Where next?