Skip to content
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

A Loss Encodes What Counts as Error

A loss function turns a prediction and target into an optimization signal. Its choice expresses assumptions about noise, error cost, class structure, and what tradeoffs the model should make.

Three Common Losses

LossFormTypical use
Mean squared errorRegression when large residuals should receive quadratic weight; connected to Gaussian-noise likelihood.
Mean absolute errorRegression requiring more robustness to large residuals; connected to Laplace-noise likelihood.
Cross-entropyClassification or next-token prediction with probabilistic targets.

Squaring Changes the Influence of Residuals

Under MSE, a residual of 10 contributes 100 while a residual of 1 contributes 1, so large residuals can dominate the fit. MAE grows linearly and is less sensitive to extreme residuals, although it has a nondifferentiable point at zero and answers a different statistical question.

Cross-entropy rewards probability assigned to the observed class, but minimizing it does not guarantee calibrated probabilities. Calibration depends on data, model specification, regularization, distribution shift, and sometimes post-hoc methods such as temperature scaling.

The loss tells the optimizer what to improve. Choose it for the task and assumptions; then evaluate separate properties such as calibration with separate diagnostics.

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.