TExcelTAD
Home › Math for Machine Learning › Empirical Risk Minimization

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 {(xn,yn)}n=1N\{(\mathbf{x}_n, y_n)\}_{n=1}^N house features and prices
hypothesis class f(⋅,θ)f(\cdot, \boldsymbol{\theta}), θ∈RD\boldsymbol{\theta} \in \mathbb{R}^D linear models θTx\boldsymbol{\theta}^T\mathbf{x}
loss function ℓ(y,y^)\ell(y, \hat{y}) squared loss (y−y^)2(y - \hat{y})^2

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:

Rtrue(θ)=Ex,y[ℓ(y,f(x,θ))],Remp(θ)=1N∑n=1Nℓ(yn,f(xn,θ))\mathbf{R}_{\text{true}}(\boldsymbol{\theta}) = \mathbb{E}_{\mathbf{x}, y}\big[\ell(y, f(\mathbf{x}, \boldsymbol{\theta}))\big], \qquad \mathbf{R}_{\text{emp}}(\boldsymbol{\theta}) = \frac{1}{N}\sum_{n=1}^{N} \ell(y_n, f(\mathbf{x}_n, \boldsymbol{\theta}))

ERM picks θ∗=arg⁡min⁡θRemp(θ)\boldsymbol{\theta}^* = \arg\min_{\boldsymbol{\theta}} \mathbf{R}_{\text{emp}}(\boldsymbol{\theta}).

Common losses

Task Loss Formula
regression squared (y−y^)2(y - \hat{y})^2
regression, robust absolute ∣y−y^∣\lvert y - \hat{y} \rvert
binary classification, y∈{−1,+1}y \in \{-1, +1\} logistic log⁡(1+e−yy^)\log(1 + e^{-y\hat{y}})
binary classification hinge (SVM) max⁡(0,1−yy^)\max(0, 1 - y\hat{y})
multi-class cross-entropy −log⁡py-\log p_y

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.

python · try it yourself
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 θ\boldsymbol{\theta}, the empirical risk is an average of independent terms, so it estimates the true risk with error O(1/N)O(1/\sqrt{N}) (Expectation and Variance). The catch is that θ∗\boldsymbol{\theta}^* 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 fit method in scikit-learn performs ERM (often with regularization) for a particular loss and hypothesis class.
  • train_test_split creates 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 (2,0,5)(2, 0, 5) for targets (3,0,1)(3, 0, 1). What is its empirical risk under the squared loss and under the absolute loss?

Answer

Squared: 173≈5.67\frac{17}{3} \approx 5.67. Absolute: 53≈1.67\frac{5}{3} \approx 1.67.

Solution

Errors (−1,0,4)(-1, 0, 4). Squared: (1+0+16)/3(1 + 0 + 16)/3. Absolute: (1+0+4)/3(1 + 0 + 4)/3. The squared loss is dominated by the one big error, which is why it is sensitive to outliers.

Exercise 2: Hinge loss

For y=+1y = +1, compute the hinge loss for scores y^=2,0.5,−1\hat{y} = 2, 0.5, -1.

Answer

00, 0.50.5, 22.

Solution

max⁡(0,1−yy^)\max(0, 1 - y\hat{y}): max⁡(0,−1)=0\max(0, -1) = 0, max⁡(0,0.5)=0.5\max(0, 0.5) = 0.5, max⁡(0,2)=2\max(0, 2) = 2. A correct prediction with too small a margin (0.50.5) is still penalized.

Exercise 3: Surrogate losses (code)

Plot-free check: for margins m=yy^∈{−2,−0.5,0,0.5,2}m = y\hat{y} \in \{-2, -0.5, 0, 0.5, 2\}, 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
python · try it yourself
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…

  1. minimize the average loss on the training data
  2. minimize the loss on the test data
  3. maximize the number of parameters
  4. make the loss zero on new data

Why do classifiers minimize the logistic or hinge loss instead of the zero-one loss?

  1. The zero-one loss is wrong
  2. The zero-one loss has zero gradient almost everywhere and is non-convex
  3. They give higher accuracy by definition
  4. The zero-one loss is too slow to compute

Training error keeps falling while test error rises. This is…

  1. underfitting
  2. overfitting
  3. a bug in the loss
  4. convergence