Search

Search papers and lessons

Empirical Risk Minimization

Level 3 · 11 min read

Empirical risk minimization trains a model by choosing the rule that does best on the training examples, in the hope that it also does well on new ones.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Learning basics
  • Losses & regularization
  • Sign in to see your progress

TL;DR

A supervised model is a rule that maps inputs to predictions. The goal is to do well on all future inputs, but you can only measure performance on the examples you have. Empirical risk minimization (ERM) picks the rule that makes the average loss on those training examples as small as possible, and it hopes that this choice also works on new data.

The hope has conditions. The rule must not be so flexible that it memorises the training set, and the training set must be a fair sample of the cases you care about.

Where you'll see it: almost every supervised training run, from fitting a straight line to a squared-error regression to training a large neural network with a cross-entropy loss.

Intuition

Start from a question you already know

Suppose you want to predict tomorrow's temperature from today's. You do not know the real rule that connects the two, and you never will see every possible day. What you have is a notebook of past days. A natural plan is to pick the simple rule that would have made the fewest mistakes on the notebook, and use it going forward. That plan is ERM.

The catch is that the notebook is a sample. A rule can be tuned to the notebook's quirks, such as one unusually hot afternoon, and then fail on tomorrow. Most of the theory of ERM is about when the notebook's average error is a reliable guide to the average error on new days.

A fair test is an average over many examples

Think of a school exam. A student who memorised last year's paper scores perfectly on it and badly on a new paper. The exam score measured memory, not understanding. ERM has the same risk: the training score can be perfect for the wrong reason. The sample must be large and representative, and the rules you consider must be simple enough that doing well on the sample says something real.

The math

Setting

The data are pairs (X,Y)(X, Y) drawn from an unknown Joint Distribution P(X,Y)P(X, Y). Here XX is the input (a temperature history, an image) and YY is the answer we want. A model is a function ff from a fixed family F\mathcal{F}, such as all straight lines. A Loss Function L(y,y^)L(y, \hat{y}) scores one prediction against its true answer, with y^=f(x)\hat{y} = f(x).

The true risk

The quantity we care about is the average loss over the whole distribution:

R(f)=E[L(Y,f(X))].R(f) = \mathbb{E}\big[L\big(Y, f(X)\big)\big].

Here E\mathbb{E} is the expectation, an average over every possible pair weighted by how likely it is. This is the risk on future data. We cannot compute it, because we do not know P(X,Y)P(X, Y).

The empirical risk

We have NN training pairs (x1,y1),…,(xN,yN)(x_1, y_1), \dots, (x_N, y_N), assumed to be drawn from the same distribution. The empirical risk replaces the expectation with an average over those pairs:

R^N(f)=1N∑i=1NL(yi,f(xi)).\hat{R}_N(f) = \frac{1}{N} \sum_{i=1}^{N} L\big(y_i, f(x_i)\big).

ERM chooses the rule with the smallest empirical risk:

f^=arg⁡min⁡f∈FR^N(f).\hat{f} = \arg\min_{f \in \mathcal{F}} \hat{R}_N(f).

Everything in the procedure can be computed from the data. That is the whole point.

Why averaging is allowed: the law of large numbers

For a fixed rule ff chosen before seeing the data, the Law of Large Numbers says that R^N(f)\hat{R}_N(f) approaches R(f)R(f) as NN grows. The average of independent samples settles near its expected value. So the training average is a sound estimate of the true risk for any single rule.

The subtlety is that ERM does not fix ff in advance. It searches over F\mathcal{F} using the same data it then evaluates on. The best rule on the sample is the one whose sample error happened to be luckiest.

The generalization gap

Define the gap

G(f^)=R(f^)−R^N(f^).G(\hat{f}) = R(\hat{f}) - \hat{R}_N(\hat{f}).

It is almost always positive: the chosen rule looks better on its training data than on new data. ERM works well when this gap is small. Two things control it. The first is the size NN: more data shrinks the gap. The second is the size of the family F\mathcal{F}: a richer family can fit the noise better, so the gap grows. This trade-off is the same one described by bias and variance and by Overfitting.

flowchart LR
  D[Training pairs] --> E[Average loss on the pairs]
  F[Family of rules] --> E
  E --> G[Pick the rule with the smallest average]
  G --> H[Check on new data]
  H -->|gap is small| I[Good generalization]
  H -->|gap is large| J[Overfitting]

Worked examples

Example
1. A constant prediction on four numbers

Take the family of constant predictions f(x)=cf(x) = c with squared loss, and training answers y=1,2,3,6y = 1, 2, 3, 6.

  1. The empirical risk is R^(c)=14∑(yi−c)2\hat{R}(c) = \frac{1}{4}\sum (y_i - c)^2.
  2. Try c=2c = 2: the errors are −1,0,1,4-1, 0, 1, 4, squares 1,0,1,161, 0, 1, 16, total 18, so R^=4.5\hat{R} = 4.5.
  3. Try c=3c = 3: the errors are −2,−1,0,3-2, -1, 0, 3, squares 4,1,0,94, 1, 0, 9, total 14, so R^=3.5\hat{R} = 3.5.
  4. The minimum is where the derivative ∑(yi−c)\sum(y_i - c) is zero, which gives cc equal to the sample mean, (1+2+3+6)/4=3(1+2+3+6)/4 = 3.

Interpretation: ERM with this family just returns the average of the answers. Only the training data enter the choice, and the average is the rule that minimises the training error.

Example
2. A memoriser that wins on the training set

Let the family be "any function at all", and let f^\hat{f} return the training answer at each training input, and 0 everywhere else. Its training pairs are (xi,yi)(x_i, y_i) with yi=1y_i = 1 for all four points.

  1. Training risk: each training point is predicted exactly, so R^N(f^)=0\hat{R}_N(\hat{f}) = 0.
  2. Now take four new inputs, none of them in the training set, each with true answer y=1y = 1. The memoriser predicts 0 for all of them.
  3. Squared loss on each new point is (1−0)2=1(1 - 0)^2 = 1, so the true risk estimate is R=1R = 1.
  4. The generalization gap is G=1−0=1G = 1 - 0 = 1.

Interpretation: ERM as stated cannot tell this rule apart from a good one using the training data alone. The fix is to restrict the family, or to measure risk on held-out data, which is the purpose of a validation set (see Train, Validation and Test Sets).

024681000.20.40.60.811.2model flexibility (illustrative)error
  • training error
  • error on new data
Illustrative curves, not measured data. Training error falls as flexibility grows; error on new data is lowest at a middle flexibility, marked here, and rises after it.
Example
3. A realistic classifier (illustrative numbers)

A spam classifier is trained by minimising the average cross-entropy on N=10000N = 10000 labelled emails, reaching an average training loss of 0.310.31. On a held-out set of 20002000 emails it reaches an average loss of 0.380.38.

  1. The training estimate is R^N(f^)=0.31\hat{R}_N(\hat{f}) = 0.31.
  2. The held-out average is a fresh estimate of the risk, because the held-out emails played no part in choosing f^\hat{f}. Take it as R(f^)≈0.38R(\hat{f}) \approx 0.38.
  3. The estimated generalization gap is 0.38−0.31=0.070.38 - 0.31 = 0.07.
  4. Gaps like this are the reason to keep a test set out of training. Had the held-out loss been 0.90.9, the gap would signal that the family was too flexible or the data too few.

Interpretation: ERM gave a model whose training error is a little optimistic, which is normal. The size of the gap, not the training loss alone, tells you whether the procedure generalised. The numbers here are chosen for illustration and do not come from a real run.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Very flexible family, few examplesThe rule memorises noise; the gap is largeUse a smaller family, regularisation (a penalty that discourages extreme rules), or more data
Training data differ from real useThe average is accurate for the wrong distributionCollect data that match the target, or reweight it
Many candidate rules tried on one datasetThe best sample score is optimistic from selection aloneHold out a validation or test set, used once
Non-convex loss, such as a deep networkFinding the exact minimiser is impossible; the search stops at a local pointUse Gradient Descent with Early Stopping and check on held-out data
Rare but costly errorsThe average loss can hide themUse a loss or evaluation that weights those cases, such as a per-class score

ERM is the default because it is simple and general. Its weakness is that it never looks at new data during training, so it must be paired with a check on data it did not see.

History

The idea of choosing a rule by its average error on a sample is old. Statisticians used least squares and maximum likelihood for centuries, and those are special cases of minimising an average loss.

The general framing, that a learning rule should minimise empirical risk and that the gap between empirical and true risk must be controlled uniformly over a family of rules, came from Vladimir Vapnik and Alexey Chervonenkis in the 1960s and 1970s. Their work on uniform convergence gave the first conditions under which ERM is guaranteed to generalise, and it led to the complexity measures now called the VC dimension, a number that counts how many arbitrary labelings a family of rules can fit. Vapnik's book Statistical Learning Theory (1998) gathered this theory into one account. The term "empirical risk minimization" became standard in machine learning as those ideas spread.

Recap

  • The quantity we want is the true risk R(f)R(f), an average over the unknown distribution.
  • ERM replaces it with the training average R^N(f)\hat{R}_N(f) and minimises that over a family of rules.
  • For a single fixed rule, the Law of Large Numbers makes the training average a good estimate. The choice itself makes the training average optimistic.
  • The generalization gap R−R^NR - \hat{R}_N is what to watch. It grows with the flexibility of the family and shrinks with more data.
  • Next: Overfitting and Bias-Variance Tradeoff, which describe what a large gap looks like and how to balance it.

Check yourself

QuestionWhy can we not directly minimise the true risk R(f)R(f)?Show answer

Because it is an expectation over the unknown joint distribution P(X,Y)P(X, Y), and we only have a finite sample from it. ERM substitutes the average over the training pairs, which we can compute.

QuestionTraining answers are y=2,4,6y = 2, 4, 6. For the constant predictor f(x)=cf(x) = c with squared loss, what value of cc minimises the empirical risk?Show answer

The derivative of ∑(yi−c)2\sum (y_i - c)^2 with respect to cc is −2∑(yi−c)-2\sum (y_i - c), which is zero when cc equals the mean of the answers. The mean is (2+4+6)/3=4(2+4+6)/3 = 4, so c=4c = 4. Check: the training risk at c=4c=4 is (4+0+4)/3=8/3(4+0+4)/3 = 8/3, and at c=3c=3 it is (1+1+9)/3=11/3(1+1+9)/3 = 11/3, which is larger.

QuestionWhy does the law of large numbers not by itself justify ERM?Show answer

The law of large numbers says the average over fixed, independent samples approaches the expected value for a single fixed rule. ERM chooses its rule using the same samples it then averages over, so the chosen rule's training average is optimistic. The selection step breaks the guarantee unless the family is restricted or the check uses data that played no part in the choice.

QuestionA model has training loss 0.12 and held-out loss 0.45. Name the likely problem and one change you would try.Show answer

The generalization gap is 0.33, which is large, so the model is probably overfitting: it fits the noise in the training data. A reasonable change is to restrict the family, add regularisation, or collect more training examples, then compare the held-out loss again.

Sources