Lesson 2 of 7 · Mathematical Optimization

Loss functions

Loss functions measure how far a prediction is from the truth: MSE and MAE for regression, cross-entropy for classification, and Huber for robustness; we link each one to a probabilistic assumption.

Mathematics for machine learning Difficulty: Intermediate Study time: 45 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

A loss function \(\ell(\hat y, y)\) gives a number measuring how bad the prediction \(\hat y\) is compared with the true value \(y\). Its average over the data is what we minimize during training.

Learning objectives

  • Computing MSE and MAE
  • Computing binary cross-entropy
  • Choosing the right loss for the type of problem
  • Linking the loss to maximum likelihood

2 Why study this topic?

The choice of loss decides what the model learns: MSE punishes large errors heavily, so outliers affect it; MAE is more robust to them; and cross-entropy gives strong gradients when the classifier is confident and wrong.

3 Theoretical explanation

Regression losses

LossFormulaNote
MSE\(\frac1n\sum (y_i-\hat y_i)^2\)Smooth, sensitive to outliers
MAE\(\frac1n\sum |y_i-\hat y_i|\)Robust to outliers, not differentiable at zero
HuberQuadratic for small errors and linear for large onesCombines the advantages of both

Classification losses

Binary cross-entropy for a predicted probability \(\hat p\) and a true class \(y\in\{0,1\}\):

\[\ell = -\big[y\log\hat p + (1-y)\log(1-\hat p)\big]\]

And for several classes: \(\ell = -\sum_k y_k\log\hat p_k\) with Softmax outputs.

The probabilistic link

Minimizing MSE is equivalent to maximum likelihood under normal noise, and minimizing cross-entropy is equivalent to maximum likelihood for a Bernoulli or categorical distribution. The loss is not an arbitrary choice.

Binary cross-entropy grows without bound when the model is confident in the wrong answer

4 Mathematical definitions

Mean squared error
The average of the squared differences between the true and predicted values.
\[\mathrm{MSE} = \frac1n\sum_{i}(y_i - \hat y_i)^2\]
Binary cross-entropy
Minus the logarithm of the probability the model gave to the correct class.
\[\ell = -[y\log\hat p + (1-y)\log(1-\hat p)]\]
Huber loss
A loss that is quadratic for errors smaller than δ and linear for larger ones.
\[L_\delta(e) = \begin{cases}\frac12e^2 & |e|\le\delta\\ \delta(|e|-\frac12\delta) & |e|\gt\delta\end{cases}\]
Loss function
A function that measures how bad the model's prediction is for one sample compared with the true value.
\[\ell(\hat y,y)\]
Mean squared error
The average of the squared differences between the true and predicted values.
\[\frac1n\sum(y_i-\hat y_i)^2\]
Mean absolute error
The average of the absolute differences; it is less affected by outliers.
\[\frac1n\sum|y_i-\hat y_i|\]

5 The equations

\[\mathrm{MSE} = \frac{1}{n}\sum_{i=1}^{n}(y_i-\hat y_i)^2\]
MSE
\[\mathrm{MAE} = \frac{1}{n}\sum_{i=1}^{n}|y_i-\hat y_i|\]
MAE
\[\mathrm{CE} = -\sum_{k=1}^{K} y_k\log\hat p_k\]
Multi-class cross-entropy

6 Step-by-step worked example

MSE and MAE

Problem: The true values are \((3, -0.5, 2, 7)\) and the predictions \((2.5, 0, 2, 8)\). Compute MSE and MAE.

  1. The errors: \(0.5, -0.5, 0, -1\).
  2. The squares: \(0.25, 0.25, 0, 1\), with mean \(1.5/4 = 0.375\).
  3. The absolute values: \(0.5, 0.5, 0, 1\), with mean \(2/4 = 0.5\).

Result: \(\mathrm{MSE} = 0.375\) and \(\mathrm{MAE} = 0.5\)

A binary cross-entropy

Problem: The true class is 1 and the model gave \(\hat p = 0.8\). Compute the loss in nats.

  1. \(\ell = -\log 0.8\).

Result: \(\approx 0.2231\) nats

7 Practical application in AI

  • Regression: Predicting prices or temperatures with an MSE or Huber loss.
  • Classification: Classifying images and text with cross-entropy after Softmax.
  • Language models: They are trained on the cross-entropy of the next token, from which perplexity is computed.

8 Interactive lab linked to the lesson

Simulating gradient descent, stochastic gradient descent, Momentum and Adam on convex and non-convex functions, with control of the learning rate and views of the loss curve, the path, convergence or divergence, and local and global minima.

Open the Optimization Lab

9 Practice exercises

Review questions for reflection

  • Why is cross-entropy preferred to MSE for classification?
  • When do you choose MAE instead of MSE?

Common mistakes

  • Using MSE with Sigmoid for classification, which weakens the gradients at saturation.
  • Computing \(\log 0\) without clipping the probabilities or using a numerically stable formula.
  • Confusing the loss (for one sample) with the cost function (the average).

Practice 6 questions from the question bank for this lesson

10 Lesson summary

  • MSE for regression, cross-entropy for classification.
  • MAE and Huber are more robust to outliers.
  • Every common loss corresponds to a probabilistic assumption.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.