Empirical Risk Minimization
What does it mean for a model to "learn"? One precise answer: choose the function from a fixed family that makes the smallest average loss on the training data, hoping that this also makes a small loss on new data. That is empirical risk minimization (ERM). This lesson names its parts (hypothesis class, loss, risk) and its central danger, overfitting.
The ingredients
| Ingredient | Symbol | Example |
|---|---|---|
| training data | house features and prices | |
| hypothesis class | , | linear models |
| loss function | squared loss |
The quantity we really care about is the expected risk on the (unknown) data distribution, but we can only compute the empirical risk on the sample:
ERM picks .
Common losses
| Task | Loss | Formula |
|---|---|---|
| regression | squared | |
| regression, robust | absolute | |
| binary classification, | logistic | |
| binary classification | hinge (SVM) | |
| multi-class | cross-entropy |
The zero-one loss (1 if wrong, 0 if right) is what we often want, but it is flat almost everywhere and has no useful gradient, so we minimize a convex surrogate such as the logistic or hinge loss instead.
Overfitting: low empirical risk, high true risk
A rich enough hypothesis class can drive the empirical risk to zero by memorizing the data. The test set, held out from training, estimates the true risk.
import numpy as np
rng = np.random.default_rng(0)
f_true = lambda x: np.sin(2 * x)
x_tr = rng.uniform(-2, 2, 15); y_tr = f_true(x_tr) + 0.2 * rng.normal(size=15)
x_te = rng.uniform(-2, 2, 1000); y_te = f_true(x_te) + 0.2 * rng.normal(size=1000)
def design(x, deg): return np.vander(x, deg + 1, increasing=True)
for deg in (1, 3, 5, 9, 14):
theta = np.linalg.lstsq(design(x_tr, deg), y_tr, rcond=None)[0]
tr = np.mean((design(x_tr, deg) @ theta - y_tr) ** 2)
te = np.mean((design(x_te, deg) @ theta - y_te) ** 2)
print(f"degree {deg:2d}: train MSE {tr:.4f} test MSE {te:.4f}")degree 1: train MSE 0.2887 test MSE 0.6486
degree 3: train MSE 0.0341 test MSE 0.0970
degree 5: train MSE 0.0148 test MSE 0.0539
degree 9: train MSE 0.0102 test MSE 0.7502
degree 14: train MSE 0.0000 test MSE 198123263.7429
Training error falls steadily as the degree grows; test error first falls, then explodes. Degree 14 with 15 points interpolates the noise. The remedies are a smaller hypothesis class, more data, or regularization (MAP Estimation and Regularization), with the amount chosen by validation.
Why the empirical risk is a good proxy (sometimes)
For a fixed , the empirical risk is an average of independent terms, so it estimates the true risk with error (Expectation and Variance). The catch is that was chosen using the same data, so its training error is optimistically biased. The test set must not be used for any choice, or it becomes biased too.
In software
- Every
fitmethod in scikit-learn performs ERM (often with regularization) for a particular loss and hypothesis class. train_test_splitcreates the held-out set (Train/Test).- In deep learning, the training loop's average loss over an epoch is the empirical risk.
Exercises
Exercise 1: Compute an empirical risk
A model predicts for targets . What is its empirical risk under the squared loss and under the absolute loss?
Answer
Squared: . Absolute: .
Solution
Errors . Squared: . Absolute: . The squared loss is dominated by the one big error, which is why it is sensitive to outliers.
Exercise 2: Hinge loss
For , compute the hinge loss for scores .
Answer
, , .
Solution
: , , . A correct prediction with too small a margin () is still penalized.
Exercise 3: Surrogate losses (code)
Plot-free check: for margins , print the zero-one, logistic (base 2) and hinge losses, and confirm that both surrogates are at least the zero-one loss.
Answer
Both surrogates lie above the zero-one loss at every margin.
Solution
import numpy as np
m = np.array([-2, -0.5, 0, 0.5, 2])
zero_one = (m <= 0).astype(float)
logistic = np.log2(1 + np.exp(-m))
hinge = np.maximum(0, 1 - m)
print(np.round(np.vstack([zero_one, logistic, hinge]), 3))
print(bool(np.all(logistic >= zero_one) and np.all(hinge >= zero_one)))[[1. 1. 1. 0. 0. ]
[3.069 1.405 1. 0.684 0.183]
[3. 1.5 1. 0.5 0. ]]
True
Upper bounds matter: driving a surrogate down also drives the classification error down.
Test yourself
Empirical risk minimization chooses parameters that…
- minimize the average loss on the training data
- minimize the loss on the test data
- maximize the number of parameters
- make the loss zero on new data
Why do classifiers minimize the logistic or hinge loss instead of the zero-one loss?
- The zero-one loss is wrong
- The zero-one loss has zero gradient almost everywhere and is non-convex
- They give higher accuracy by definition
- The zero-one loss is too slow to compute
Training error keeps falling while test error rises. This is…
- underfitting
- overfitting
- a bug in the loss
- convergence