MML204

Loss Functions

โฑ7 min read๐Ÿ‘๏ธEye-Care Reading Mode

What is it?

A loss function is a formula that measures how wrong a model's predictions are. It takes the model's predictions and the true answers, and returns a single number โ€” the loss. A smaller loss means better predictions.

Why we need it

A model makes predictions. Those predictions are rarely perfect. We need a way to measure how far off they are โ€” a single number that summarizes the total error across all examples.

That number is the loss. It is the thing we minimize during training. Without a loss function, the model has no feedback and cannot improve.

The choice of loss function matters. Different loss functions penalize errors differently, and the right choice depends on your problem.


Explained in detail

Suppose you are predicting house prices. You have 4 houses. The model predicts prices, and you know the actual prices.

The loss function is like a report card. It does not grade each house separately โ€” it gives you one overall score for how well you did across all of them.

Two common loss functions for regression:

Mean Squared Error (MSE) โ€” take each error (prediction โˆ’ true), square it, then average all the squares:

MSE=1nโˆ‘i=1n(yiโˆ’y^i)2\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2

Squaring does two things: it makes all errors positive (so they do not cancel out), and it punishes large errors heavily โ€” a error of 10 counts 100 times more than an error of 1.

Mean Absolute Error (MAE) โ€” take the absolute value of each error (always positive), then average:

MAE=1nโˆ‘i=1nโˆฃyiโˆ’y^iโˆฃ\text{MAE} = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|

MAE treats all errors proportionally. An error of 10 is exactly 10 times worse than an error of 1.

Which to use?

  • Use MSE when large errors are especially bad (e.g. medical predictions).
  • Use MAE when you want to be robust to outliers โ€” one very wrong prediction does not dominate.

Loss Functions โ€” MSE vs MAE

Prediction:3.78
MSE:0.798
MAE:0.704
Points:5
Presets:

Drag any point up or down to change its value. Add an outlier to see how MSE and MAE respond differently.

What to try

  • Drag any data point up or down to change its true value. Watch the MSE and MAE update live.
  • Add an outlier using the preset โ€” drag one point very far from the others. Notice how MSE shoots up much more than MAE. That is because MSE squares the error.
  • Switch between MSE and MAE using the toggle to see which metric changes more for the same data.
  • The orange line is the model's current prediction (a horizontal mean line). The vertical bars show individual errors.

Notes & Formula

Mean Squared Error (MSE)

LMSE=1nโˆ‘i=1n(yiโˆ’y^i)2L_\text{MSE} = \dfrac{1}{n} \displaystyle\sum_{i=1}^{n} (y_i - \hat{y}_i)^2

Mean Absolute Error (MAE)

LMAE=1nโˆ‘i=1nโˆฃyiโˆ’y^iโˆฃL_\text{MAE} = \dfrac{1}{n} \displaystyle\sum_{i=1}^{n} |y_i - \hat{y}_i|

SymbolMeaning
nNumber of data points (examples)
yแตขTrue value for the i-th example
ลทแตขPredicted value for the i-th example (ลท = "y-hat")
yแตข โˆ’ ลทแตขThe error (residual) for the i-th example
(ยท)ยฒSquaring makes the error positive and penalizes large errors heavily
|ยท|Absolute value makes the error positive without extra squaring
๐Ÿ’ก

MSE is the most commonly used loss for regression because it is smooth and easy to differentiate โ€” which makes gradient descent work cleanly.

โš ๏ธ

Common mistake: confusing loss (training error) with accuracy (a separate evaluation metric). Loss is computed during training to guide updates. Accuracy is computed separately to evaluate the final model.


Quiz

๐Ÿงฉ

Ready to test yourself?

10 questions ยท explanations shown after

๐ŸŽฏ

Mastered this concept?

Tick this box when you feel confident explaining it.

๐Ÿ“
Study Notes & Files0 files

Topic Resources

GitHubโ†—

๐Ÿ—‚๏ธ

No notes uploaded for this topic yet

Lecture notes, summaries, and notebook exercises will be added here. You can also view or contribute notes on GitHub.

Was this clear?