Empirical Risk Minimization: The Principle Behind Most Training
See what training minimizes, why average loss on a sample can stand in for future loss, and where that substitution breaks down.
Key takeaways
- True risk is expected loss over the data distribution; empirical risk is average loss over a sample.
- ERM picks the model in a chosen class that has the lowest empirical risk.
- The choice of loss decides what the best prediction is, such as a mean, a median or a probability.
- Minimizing over a very flexible class can drive empirical risk to zero while true risk stays high.
- Error splits into approximation, estimation and optimization parts, and each has a different remedy.
Empirical risk minimization (ERM) means choosing the model with the smallest average loss on your training examples. You care about loss on future data, which you cannot measure, so you minimize loss on the data you have and hope the two stay close. Linear regression, logistic regression and most neural network training are variations on this idea.
The principle is simple. The interesting questions are when the hope is justified, which loss to average, and which set of models to search.
Two kinds of risk
You have inputs x, targets y and a model f that outputs a prediction ŷ = f(x). A loss function L(y, ŷ) scores how bad that prediction is. Assume pairs (x, y) come from some unknown probability distribution.
The true risk is the average loss over that whole distribution:
R(f) = E[ L(y, f(x)) ] expectation over new (x, y)
R̂(f) = (1/n) ∑ L(yᵢ, f(xᵢ)) average over n training examples
f̂ = the f in H with the smallest R̂(f) H is the model class The set H is the hypothesis space: all lines, all trees of depth three, or all weight settings of a particular network.
If you fix a model f before seeing the data, and the examples are independent draws from the distribution, then R̂(f) is an unbiased estimate of R(f). It gets more accurate as n grows. The catch is that f̂ is chosen using the same data, so its empirical risk is no longer an unbiased estimate. It tends to be too low.
The loss decides what you learn
Consider the simplest model class of all: predict the same constant c for every input. Suppose the four targets are 2, 3, 4 and 11.
Under squared loss, the empirical risk is the average of (y − c)². It is minimized at the mean, c = 5. The squared errors are 9, 4, 1 and 36, so the risk is 50/4 = 12.5.
Under absolute loss, the average of |y − c| is minimized at a median, which here is any value from 3 to 4. At c = 3.5 the errors are 1.5, 0.5, 0.5 and 7.5, giving 10/4 = 2.5. At c = 5 the absolute risk would be (3 + 2 + 1 + 6)/4 = 3.
Same data, same model class, different answers. The outlier at 11 pulls the squared-loss solution upward and barely affects the median. Choosing a loss is choosing what "good" means, a theme developed in loss functions as definitions.
| Loss | Best constant prediction | Typical use |
|---|---|---|
| Squared error | Mean of the targets | Regression where large misses matter a lot |
| Absolute error | Median of the targets | Regression with outliers |
| Log loss (binary) | Fraction of positive labels | Probability estimates |
| Zero-one loss | Most common class | The error rate you often report |
Surrogate losses for classification
For classification you often want to minimize the zero-one loss: 1 for a wrong label, 0 for a right one. Its average is the error rate. It is hard to optimize directly, because it is flat almost everywhere and jumps at the decision boundary. Gradients tell you nothing.
So training uses a surrogate loss, a smoother or convex stand-in. With labels y in {−1, +1} and a real-valued score s, the margin is m = y·s, and common choices are:
- Logistic loss: ln(1 + e^(−m)), used in logistic regression.
- Hinge loss: max(0, 1 − m), used in support vector machines.
- Exponential loss: e^(−m), connected with AdaBoost.
Each penalizes negative margins heavily and rewards confident correct scores. Lower surrogate loss usually goes with lower error, but not in lockstep. You can see validation log loss rise while accuracy holds steady, when a few predictions become confidently wrong.
Maximum likelihood is a special case
If your model outputs a probability p(y | x; θ), the negative log-likelihood of the training set is a sum of per-example terms −ln p(yᵢ | xᵢ; θ). Dividing by n gives an empirical risk. Maximizing likelihood and minimizing this risk are the same optimization.
With Gaussian noise of fixed variance, this becomes squared error. With a Bernoulli label, it becomes binary cross-entropy. The derivation appears in deriving cross-entropy from likelihood.
A tiny ERM in code
Here the class H is every threshold rule on one feature: predict 1 when x ≥ t. The code tries all candidate thresholds and keeps the one with the fewest training errors.
import numpy as np
rng = np.random.default_rng(1)
def sample(n):
x = rng.uniform(0, 1, n) # shape (n,)
y = (x >= 0.6).astype(int)
flip = rng.uniform(size=n) < 0.1 # 10% label noise
return x, np.where(flip, 1 - y, y)
x, y = sample(50)
candidates = np.concatenate(([0.0], np.sort(x), [1.01]))
risks = [np.mean((x >= t) != y) for t in candidates]
best_t = candidates[int(np.argmin(risks))]
x_new, y_new = sample(100000)
print(best_t, min(risks), np.mean((x_new >= best_t) != y_new)) Because labels are flipped with probability 0.1, no rule can have true risk below 0.1. Try n = 10, then n = 1,000, and watch the learned threshold and the gap between the two risks.
When minimizing the average goes wrong
Imagine H contains every possible function. One of them stores the training table and returns the stored label, and outputs 0 for anything new. Its empirical risk is zero whenever no input appears with two different labels. Its true risk can be terrible. This is overfitting in its purest form.
Three standard defenses exist.
- Restrict H. Use fewer features, shallower trees or a simpler family.
- Penalize complexity. Minimize R̂(f) + λ·Ω(f), where Ω measures something like the size of the weights and λ sets its strength. This is regularized ERM. Choosing among nested classes with a penalty for size is called structural risk minimization.
- Check on held-out data. Pick H, λ or the stopping time using a validation set.
ERM also inherits every flaw in the sample. If some group is rare, its errors barely move the average. If the data are collected differently from where the model will run, you are minimizing risk under the wrong distribution. Reweighting examples can help when you know how the distributions differ.
Weighting the average
Plain ERM gives every training example the same vote, 1/n. You can change the votes with weighted ERM, which minimizes the sum of wᵢ·L(yᵢ, f(xᵢ)) divided by the sum of the weights wᵢ.
Weights are useful in several situations. If you sampled one class heavily to save labeling effort, weighting each example by the inverse of its sampling probability recovers an estimate of risk under the original population. If one kind of mistake costs five times as much, a larger weight on those examples says so. If a few examples carry very large weights, though, they dominate the average and the effective amount of data shrinks.
Take two examples with losses 0.2 and 1.0. With equal weights the average is 0.6. Give the second a weight of 3 and it becomes (0.2 + 3.0)/4 = 0.8. The optimizer will now work harder on the second example.
Weighting changes what probability estimates mean. A classifier trained with up-weighted positives will tend to report probabilities that are too high for the original population, so recalibrate if you need the probabilities themselves.
Where the remaining error comes from
Let f* be the best possible predictor of any form, and f_H the best one inside H. The excess risk of the model you actually obtain, f̃, splits into three pieces:
R(f̃) − R(f*) = [R(f_H) − R(f*)] approximation error: H is too limited
+ [R(f̂) − R(f_H)] estimation error: finite sample
+ [R(f̃) − R(f̂)] optimization error: search stopped short A richer H lowers approximation error and tends to raise estimation error. More data lowers estimation error. Better optimization lowers the last term, which can even be negative in effect when stopping early happens to help on new data.
There is also a neat bound on estimation error. Suppose every f in H has |R(f) − R̂(f)| ≤ ε. Then R(f̂) ≤ R̂(f̂) + ε ≤ R̂(f_H) + ε ≤ R(f_H) + 2ε. The middle step holds because f̂ has the smallest empirical risk. Controlling the gap uniformly over H is therefore enough, and that is the starting point of PAC learning.
Common questions
Is gradient descent the same as ERM?
No. ERM states what you want: a minimizer of average loss. Gradient descent is one way to search for it. With neural networks the search usually does not find an exact global minimum, and which solution it lands on affects how well the model generalizes.
Does ERM require independent examples?
The procedure runs on any data. The guarantees that link empirical and true risk usually assume independent draws from one distribution, or some weaker substitute for that assumption.
Why not minimize validation loss directly?
Once you optimize against a data set, it is training data in effect. Validation data stay informative only because you use them for a small number of choices.