Loss Functions and the Learning Objective
P6.nn-foundations.04 · Audience: guest, language-pro, it-ml · Prerequisites: The Feedforward Network
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.
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
ⓘ 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 is the average uncertainty of a distribution — equivalently, the minimum expected number of bits (with ) needed to encode samples drawn from . It is maximised by the uniform distribution (every outcome equally likely = maximal surprise) and is when puts all mass on one outcome.
Cross-entropy is the expected number of bits to encode data drawn from using a code optimised for . This is the standard classification loss: is the one-hot label vector and is the model's predicted probability distribution, so collapses to the seen above.
KL divergence measures the extra bits incurred by coding with instead of :
It is (Gibbs' inequality), zero iff , and asymmetric — in general — so it is a divergence, not a true distance.
Key ML link. Writing for the empirical data distribution and for the model, minimising cross-entropy loss equals minimising , because and the entropy is constant in . Both are therefore identical to maximum-likelihood estimation of the model parameters.
Mutual information quantifies how much knowing reduces uncertainty about :
It is the KL divergence between the joint distribution and the product of the marginals, is symmetric, and is zero iff and 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.
| Type | Resource |
|---|---|
| Video | StatQuest with Josh Starmer — Cross Entropy, Clearly Explained! and Mean Squared Error (YouTube) |
| Video | 3Blue1Brown — What is backpropagation really doing? (Deep Learning, Chapter 3, YouTube) |
| Course | Coursera — Deep Learning Specialization, Course 1 Week 2: Logistic Regression as a Neural Network (free audit) |
| Reference | Goodfellow, 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.
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?
Later in Neural-Network Foundations
This module unlocks