Loss Functions
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:
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 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
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
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.
Topic Resources
๐๏ธ
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?