← All notes
Course chapters
On this page

Lesson 10 · Neural networks

Loss functions

How can prediction errors become one number? Explore squared error, cross-entropy and the gradients that guide learning.

1. Both predictions are wrong — but equally wrong?

Think of a number-guessing game. Someone chooses an integer between 1 and 100. You guess 50 and hear “too low”; you guess 88 and hear “too high”. Each answer tells you which way to move, so the search becomes more focused.

150?88100The target is hidden from the player50: too low88: too highFeedback narrows the search instead of leaving each guess blind.
Figure 10-1. A guessing game gives a direction: increase a guess that is too low, decrease one that is too high. A loss and its derivatives provide numerical feedback for learning.

Lesson 9 described training as repeatedly adjusting a network's parameters. But a network may have thousands of adjustable values and may output a whole probability distribution. What feedback tells it how to improve?

We give it a loss function: a rule that compresses how a prediction compares with the target into one numerical objective. Differentiating that objective with respect to the parameters gives a direction for updates. The optimiser and learning rate determine how to take those updates; the loss value itself is not an update step.

Start with a simpler task: predicting a continuous value. Suppose a house actually costs €300,000. Prediction A is €299,000; prediction B is €100,000. Both are wrong, but B is much further away. A useful objective must express that difference, rather than only returning “wrong”.

For easier arithmetic, measure prices in thousands of euros: the target is y=300y=300, and the predictions are y^=299\hat y=299 and y^=100\hat y=100.

Pause and think

Why not simply use prediction minus target as the loss, and average that difference over the examples?

I've thought about it — show the answerHide answer

Positive and negative errors can cancel. A prediction that is 10 too high and one that is 10 too low have average signed error zero, even though both are wrong. Minimising a signed difference also rewards increasingly negative predictions instead of agreement with the target. We need a measure that penalises deviations on either side.

Taking an absolute value is one option; taking a square is another. The square gives a smooth objective and penalises larger deviations more strongly.

2. Square the error: mean squared error

Square each prediction error, then average over nn examples. This is mean squared error (MSE):

LMSE=1n∑i=1n(y^i−yi)2.L_{\mathrm{MSE}}=\frac{1}{n}\sum_{i=1}^{n}(\hat y_i-y_i)^2.

The hat marks a prediction; yiy_i is the observed target. For the two house predictions:

(299−300)2=1,(100−300)2=40,000.(299-300)^2=1,\qquad (100-300)^2=40{,}000.

An error 200 times as large receives a squared penalty 40,000 times as large. These numbers use our chosen price units: scaling the targets changes the numerical loss.

yxPrediction lineSquares illustrate error magnitude, not extra observations.
Figure 10-2. Each vertical residual is the difference between an observed value and the line's prediction. A square with that residual as its side illustrates the squared error; larger errors contribute more to MSE.

The gradient supplies the feedback

For one example, hold the true value yy fixed and differentiate the squared error with respect to the prediction:

L(y^)=(y^−y)2,dLdy^=2(y^−y).L(\hat y)=(\hat y-y)^2, \qquad \frac{dL}{d\hat y}=2(\hat y-y).
  • If the prediction is too high, this derivative is positive. A small move towards a lower prediction reduces the loss.
  • If it is too low, the derivative is negative. A small move towards a higher prediction reduces the loss.
  • The further the prediction is from the target, the larger the magnitude of this derivative.

For the averaged loss, each prediction's derivative has the extra factor 1/n1/n:

∂LMSE∂y^i=2n(y^i−yi).\frac{\partial L_{\mathrm{MSE}}}{\partial\hat y_i} =\frac{2}{n}(\hat y_i-y_i).

The network does not usually adjust predictions directly. It adjusts parameters θ\theta, so the derivative must continue through the network by the chain rule:

∇θL=∂L∂y^∇θy^.\nabla_\theta L =\frac{\partial L}{\partial\hat y}\nabla_\theta\hat y.

MSE is a common objective for regression: predicting quantities such as prices, temperatures or demand. Other regression losses are useful too; squared error can be sensitive to outliers.

3. Squared error for classification: a weak signal

Return to our fruit classifier. In a binary problem, let y=1y=1 mean apple and let pp be the predicted probability of apple. Compare:

p=0.9:(p−1)2=0.01,p=0.01:(p−1)2=0.9801.p=0.9:\quad (p-1)^2=0.01, \qquad p=0.01:\quad (p-1)^2=0.9801.

The confidently wrong prediction receives roughly 98 times the loss. But training uses gradients, not just the size of the loss.

Differentiate with respect to the probability:

dLdp=2(p−1).\frac{dL}{dp}=2(p-1).

At p=0.01p=0.01, this is −1.98-1.98; at p=0.001p=0.001, it is −1.998-1.998. As the probability of the correct answer approaches zero, the magnitude approaches two rather than growing without bound.

Follow the derivative through Sigmoid

That probability derivative is only part of the picture. Our binary classifier produces a raw score, or logit, zz, and then applies Sigmoid:

p=σ(z),dpdz=p(1−p).p=\sigma(z),\qquad \frac{dp}{dz}=p(1-p).

Combining the derivatives gives:

dLMSEdz=2(p−y)p(1−p).\frac{dL_{\mathrm{MSE}}}{dz}=2(p-y)p(1-p).

For a true apple, y=1y=1, and a confidently wrong prediction with pp close to zero, the factor p(1−p)p(1-p) is very small. The gradient with respect to the score therefore becomes small precisely when the prediction badly needs correcting.

For example:

p=0.01:dLdz=−0.019602,p=0.001:dLdz=−0.001996002.p=0.01:\quad \frac{dL}{dz}=-0.019602, \qquad p=0.001:\quad \frac{dL}{dz}=-0.001996002.

This is a reason cross-entropy is commonly paired with Sigmoid for binary classification. MSE can be used on probabilities, but the resulting optimisation behaviour differs.

Pause and think

For a certain correct label, what function of its predicted probability is zero at one, but grows without bound as the probability approaches zero?

I've thought about it — show the answerHide answer

The negative logarithm has exactly this shape:

L=−ln⁡p.L=-\ln p.

It is zero at p=1p=1 and grows without bound as pp approaches zero from above. Its derivative with respect to pp is −1/p-1/p. Lesson 6 introduced the same idea as the surprise associated with an event: a true answer that the model considers unlikely is highly surprising.

4. Logarithmic loss: cross-entropy

Let pcp_c be the probability assigned to the correct class. For a single example with a known class label, cross-entropy is:

LCE=−ln⁡pc.L_{\mathrm{CE}}=-\ln p_c.

We use the natural logarithm here. Lesson 6 used base-two logarithms to measure information in bits; changing the base scales the objective by a constant.

For a correct class probability of 0.90.9, the loss is approximately 0.1050.105. At 0.010.01, it is approximately 4.6054.605; at 0.0010.001, approximately 6.9086.908. A tenfold reduction in probability adds ln⁡10\ln 10 to the loss. The growth is logarithmic, not exponential.

012345Loss
LCE=−ln⁡pL_{\mathrm{CE}}=-\ln p
LMSE=(p−1)2L_{\mathrm{MSE}}=(p-1)^2
00.250.50.751Probability assigned to the correct class
Figure 10-3. For a true label of one, squared error approaches one as the predicted probability approaches zero; negative log probability grows without bound. The graph starts at 0.01 because the logarithm is undefined at zero.

Probability gradients and score gradients are different

With respect to correct-class probability:

dLdpc=−1pc.\frac{dL}{dp_c}=-\frac{1}{p_c}.

Its magnitude is 100 at pc=0.01p_c=0.01 and 1,000 at pc=0.001p_c=0.001. However, this does not mean the network's parameter gradients automatically become infinitely large.

For binary targets, the complete loss is:

LBCE=−[yln⁡p+(1−y)ln⁡(1−p)].L_{\mathrm{BCE}}=-\bigl[y\ln p+(1-y)\ln(1-p)\bigr].

For the same Sigmoid score p=σ(z)p=\sigma(z), applying the chain rule simplifies the score derivative to:

dLBCEdz=p−y.\frac{dL_{\mathrm{BCE}}}{dz}=p-y.

When y=1y=1 and pp is close to zero, this derivative stays close to −1-1, rather than disappearing through the extra Sigmoid factor found in squared error. That is the useful comparison: cross-entropy preserves a corrective signal at the score even for a confident mistake. Parameter gradients still depend on the rest of the network.

Try it on the fruit machine

Choose whether the example really is an apple, then adjust the four features. Watch how the loss changes as the machine assigns more or less probability to that label.

SmallLarge
Less redMore red
SourSweet
DryJuicy
True label
012345Loss01Probability assigned to the true label
pc={py=11−py=0,L=−ln⁡pcp_c=\begin{cases}p&y=1\\1-p&y=0\end{cases},\qquad L=-\ln p_c

Predicted probability of apple: 55.0%

Probability of the true label: 55.0%

Cross-entropy loss: 0.598

Experiment 10-A. Choose the true label and change the four input features. The dot follows negative log probability: confident correct predictions have low loss, confident wrong predictions have high loss. The toy classifier's weights stay fixed; this is a loss experiment, not an automatic training run.

If the true label is apple, pc=pp_c=p. If it is not apple, pc=1−pp_c=1-p. Changing the true label leaves the model's prediction unchanged but changes the loss: an objective compares a prediction with a target, rather than judging a prediction in isolation.

The feature controls use the same illustrative, fixed classifier as Lesson 7. No parameters are being trained in this experiment.

More than two classes

For KK mutually exclusive classes, with predicted probabilities p^k\hat p_k and target distribution yky_k:

LCE=−∑k=1Kykln⁡p^k.L_{\mathrm{CE}}=-\sum_{k=1}^{K}y_k\ln\hat p_k.

For a one-hot target, the correct class cc has yc=1y_c=1 and the other entries are zero. Only the correct-class term remains:

LCE=−ln⁡p^c.L_{\mathrm{CE}}=-\ln\hat p_c.

Average this loss across the batch to obtain the training objective. If targets are soft distributions instead of one-hot labels, more than one term contributes.

Cross-entropy is a common classification objective. Softmax, in the next lesson, turns arbitrary scores into a distribution over mutually exclusive classes. Binary classification often uses Sigmoid instead. Practical implementations commonly compute the loss directly from logits using numerically stable expressions rather than separately taking the logarithm of rounded probabilities.

5. What you have discovered

A loss function makes prediction quality a numerical objective that an optimiser can work with.

  • MSE averages squared errors and is common for continuous-value regression.
  • Gradients describe how the objective changes. Parameter updates depend on the chain rule, the optimiser and the learning rate.
  • Sigmoid plus MSE can produce a small score gradient for a confidently wrong prediction.
  • Cross-entropy uses negative log probability. It rewards assigning probability to the true label and penalises confident mistakes.
  • For Sigmoid plus binary cross-entropy, the score gradient simplifies to p−yp-y.
  • The objective must fit the task, targets and outputs. No loss guarantees good generalisation or successful training on its own.

We have now connected the prediction to an objective. Next: where do class probabilities come from when the network outputs arbitrary scores?

Check your understanding

1. What does a loss function do during training?
2. What should mainly guide the choice of loss function?