C.W.K.
Stream
Lesson 02 of 06 · published

Loss Functions: The Rulers We Optimize

~10 min · loss, MSE, MAE, cross-entropy

Level 0Math Novice
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

Loss = "How Wrong Are We?"

Every learning algorithm needs a number that says how badly the model is doing. That number is called loss, and the choice of loss function is one of the most important decisions in ML — it shapes what the model considers "wrong."

Three Workhorses

LossFormulaUse when
Mean Squared Error (MSE)Regression. Penalizes big errors heavily.
Mean Absolute Error (MAE)Regression with outliers. Treats all errors linearly.
Cross-EntropyClassification. Probabilistically grounded.

MSE vs MAE — Why the Squaring Matters

MSE squares the error, so a residual of 10 contributes 100 while a residual of 1 contributes only 1. The model learns to desperately avoid big errors, even at the cost of being slightly worse on small ones. Good when small errors are forgivable but big ones are catastrophic. Bad when you have outliers (the squaring makes outliers dominate the loss).

MAE treats every error linearly. A residual of 10 contributes 10. The model is more tolerant of outliers but cares less about small refinements.

The loss function tells the model what to care about. Pick it intentionally — it's not a default. MSE for "no big surprises." MAE for "outliers exist and that's OK." Cross-entropy for "this is classification, calibrate probabilities."

Code

MSE vs MAE on outlier-prone data·python
import numpy as np

y_true = np.array([1.0, 2.0, 3.0, 4.0, 100.0])      # last is an outlier
y_pred = np.array([1.1, 1.9, 3.2, 3.8, 5.0])

mse = np.mean((y_true - y_pred) ** 2)
mae = np.mean(np.abs(y_true - y_pred))
print(f"MSE: {mse:.3f}")    # huge — outlier dominates
print(f"MAE: {mae:.3f}")    # large but not as catastrophic
Cross-entropy for classification·python
import torch
import torch.nn.functional as F

logits = torch.tensor([[2.0, 1.0, 0.5]])       # raw scores
target = torch.tensor([0])                      # correct class index

# Cross-entropy loss — built for classification
ce = F.cross_entropy(logits, target)
print(ce.item())                                # ~0.42

External links

Exercise

Take the outlier example. Compute the slope/intercept of a regression line minimizing MSE vs MAE. (Use scipy.optimize.minimize or numpy.linalg.lstsq for MSE, and scipy.stats.linregress won't help — write a custom loss for MAE). Notice how MSE pulls the line toward the outlier; MAE doesn't.
Hint
MSE has a closed-form (least squares). MAE doesn't — use scipy's optimizer to minimize the absolute-error loss.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.