01 / 27 · The Learning Problem
What machine learning is
Why a course on machine learning follows the probability course: learning a rule from examples is minimizing an average loss, and the hard part is not fitting the examples but doing well on new ones. The setup of features, targets, losses, risk and empirical risk; the best possible prediction; the kinds of tasks; why training error lies; and the plan of the course, with the probability each method rests on and the NLP and generative-model notes that build on it.
On this page 12
A bank decides whom to lend to, a shop how much stock to order, a search engine which ten pages to show first. None of these decisions follows from a formula derived from first principles, but each comes with a history: loans that were or were not repaid, past sales, past clicks. Machine learning turns such a history into a rule that predicts the outcome for a new case. Outside text and images, most of this work is done on tables (prices, credit, churn, demand, search ranking), and the workhorses are linear models, decision trees and gradient boosting; neural networks are built from the same parts, linear maps, losses, gradient descent and regularization. On medium-sized tabular datasets, tree ensembles still beat deep networks (Grinsztajn, Oyallon and Varoquaux, 2022). Fitting a rule to the history is the easy part. The hard part is generalization: a rule that is perfect on the examples can be useless on the next case, and a careless check can make a useless rule look perfect. This note sets up the problem in the language of the probability course, shows why the error on the training examples is optimistic, and lays out the course.
Notation. An object (a customer, a flat, a query–document pair) is described by a feature vector , usually ; its target is . The pairs come from an unknown distribution on , and the training set holds independent draws from it. A model is a function from to predictions, chosen from a family ; the trained model is . A loss is the cost of predicting when the truth is .
From examples to a rule#
The training set is a table: one row per object, one column per feature, plus the target column. To predict the price of a flat, the features might be the area, the district, the floor and the year of construction, and the target the price. A supervised learning method takes the table and returns a function that, given the features of a flat that was not in the table, returns a price.
“Good” needs a number. The loss says how bad one prediction is, for example the squared error , and the empirical risk of a model is its average loss on the training set:
Most training procedures, from least squares to gradient boosting, minimize it over a family of models: , empirical risk minimization. What we care about, though, is the loss on the next object, the risk
the expectation of the loss under the distribution that produces the data. The empirical risk is a sample average of losses, and for a model fixed before the data are seen it is an unbiased estimate of the risk. If the loss has a finite mean under , the estimate converges to the risk by the law of large numbers. If the loss also has a finite variance , the standard error is , of order , and the central limit theorem turns it into an error bar; a bounded loss satisfies both conditions, and Hoeffding’s inequality then gives a bound for every .
The trained model is not fixed before the data are seen: it was chosen because it fits them. Worked example. The simplest model ignores the features and predicts one number . Four flats cost 3, 5, 4 and 8 (millions). The squared loss is minimized by the mean, , and its empirical risk is . Is 3.5 the risk of predicting 5? Suppose the prices have mean and variance . On a new flat, the risk of the constant is , whose average over training sets is . The empirical risk is , whose average is , the biased variance estimate of the estimators note. With :
A model with a single parameter already looks better on its training data than it is, because the parameter was chosen to fit those data: the gap is . Models with thousands of parameters can make the gap as large as the risk itself.
The best possible prediction#
Which function would we choose if we knew ? For the squared loss, the answer is the conditional mean , the best predictor: for any , . Its own risk, , is the noise that the features do not explain, the irreducible error. Other losses pick out other summaries of the conditional distribution (other losses, other targets):
| Loss | Best prediction at | Likelihood reading (optional) | |
|---|---|---|---|
| Squared | mean of | Gaussian | |
| Absolute | median of | Laplace | |
| 0–1 | most probable class | ||
| Cross-entropy | categorical |
The third column needs no assumption about the noise. The conditional mean minimizes the expected squared loss for every distribution with , Gaussian or not, and the conditional median minimizes the expected absolute loss whenever . The loss decides which summary of we estimate. The last column is an optional reading from the maximum likelihood note: up to constants, the squared, absolute and cross-entropy losses are negative log-likelihoods of these noise models, so minimizing them is maximum likelihood when the model holds. With exponential noise, for example, least squares still estimates the conditional mean, but it is no longer the maximum-likelihood fit. Not every useful loss has such a reading: the 0–1 loss has none, and the hinge loss of the support vector machine is not a normalized conditional likelihood. For classification with the 0–1 loss, the best rule predicts the most probable class, the Bayes classifier, and its risk is the Bayes error.
A trained model falls short of the best in two ways. Writing more generally for the smallest possible risk under the chosen loss ( for the squared loss, the Bayes error for the 0–1 loss) and for the best member of the family,
A family that is too simple cannot approximate ; a family that is too rich for examples cannot be estimated well from them. Every method in this course chooses a point on this trade-off, and the bias–variance note makes it exact for the squared loss.
Kinds of tasks#
| Task | Target | Examples | Typical loss or metric |
|---|---|---|---|
| Regression | a number | price, demand, delivery time | squared or absolute error |
| Binary classification | one of two classes | default, churn, fraud | cross-entropy; precision, recall, ROC-AUC |
| Multiclass, multilabel | one of classes, or a set of labels | topic of an article, tags | softmax cross-entropy |
| Ranking | an order of items for a query | search results, recommendations | pairwise and listwise losses, NDCG |
| Clustering | none | customer segments | within-cluster distance |
The first four are supervised: the training set contains the targets. Ranking differs in that the loss depends on several objects at once: a list is good if the relevant documents come first, whatever the scores are. Unsupervised tasks have no target at all; besides clustering they include density estimation, anomaly detection and dimensionality reduction, the subjects of the section on learning without labels.
Fitting is easy, generalizing is hard#
The most flexible family contains all functions, and in it a perfect training fit always exists: the memorizer returns when asked about and some default elsewhere. Its empirical risk is zero. With continuous features, a new object almost never coincides with a training object, so the memorizer answers every new question with its default: it has learned nothing. A rich family overfits in a less extreme way, and the figure shows how. Polynomials of increasing degree are fitted by least squares to 12 points of a sine with noise of variance (the same setup as the bias–variance example); both curves are exact expected values over the noise:
Training error falls, error on new data does not
The training error falls with every added coefficient: 0.541 for a constant, 0.067 at degree 3, 0.015 at degree 9, and exactly 0 at degree 11, where the polynomial passes through all 12 points. The error on new data falls only while the model is too simple for the sine: it is smallest at degree 3 (0.121), and then grows to 0.208 at degree 9, 0.421 at degree 10 and 3.16 at degree 11. From degree 5 on, the polynomial can follow the sine almost exactly, and the expected training error is for coefficients: each coefficient absorbs one direction of the noise, of training error per coefficient. That absorbed noise is what the model gets wrong on new data.
Probability says how much data the check needs. For one model fixed in advance, Hoeffding’s inequality bounds the deviation of its validation loss from its risk; for the best of candidates chosen on the same validation set, the bound pays for the selection (a first generalization bound). The validation note turns this into practice: training, validation and test sets, cross-validation, and error bars on the result.
The course as a bridge#
Optional depth: how each method rests on the probability course and where the NLP and Generative Models courses use it. The rest of the note does not depend on it.
This course sits between the probability course and two courses that build on it, NLP and Generative Models. Each method is introduced with the probability it rests on, and each note says where the later courses use it. The main threads:
| Probability tool | What it becomes here | Where NLP and Generative Models use it |
|---|---|---|
| Expectation | risk as an expected loss; a minibatch gradient as an estimate of an expected gradient (gradient descent) | every training objective is an empirical risk |
| Concentration, the LLN, the CLT, the bootstrap | generalization, validation and its error bars, selection bias, metrics | evaluation and leakage in NLP |
| Conditional expectation | the target of least squares and of nearest neighbours; the bias–variance split | |
| Maximum likelihood | squared error read as a Gaussian likelihood; cross-entropy in logistic regression and the softmax; the EM algorithm | text classifiers, language models, cross-entropy and KL |
| Bayesian inference | ridge and lasso as priors (regularization); smoothed target encoding | weight decay in fine-tuning |
| The multivariate Gaussian | PCA, Gaussian mixtures, the geometry of nearest neighbours in high dimension | autoencoders and VAEs, semantic search |
Two threads start in this course itself: backpropagation, which trains the recurrent networks and Transformers of the NLP course and every network of the Generative Models course, and learning to rank, which scores the passages of semantic search and retrieval-augmented generation.
The course as a bridge: from probability to NLP and generative models
The course map#
Each step either answers a limitation of the one before it or, in the last rows, changes the task:
| Failure | Answer |
|---|---|
| Memorizing the training set does not generalize | risk and validation: this note, data, features and leakage, validation |
| Similar inputs should get similar outputs, but similarity in many dimensions is nearly meaningless | nearest neighbours and the curse of dimensionality |
| A neighbour vote has no global structure | least squares |
| The closed form is too expensive or does not exist | gradient descent, stochastic and adaptive optimizers |
| With many features the fit chases noise | regularization |
| Least squares on class labels lets easy points move the boundary | linear classifiers and surrogate losses |
| We need probabilities, not only a side of a line | logistic regression |
| Accuracy hides the errors that matter | classification metrics |
| The best separating line is not unique, and lines are too simple | support vector machines, kernels |
| More than two classes, or several labels at once | multiclass and multilabel classification |
| A line cannot express interactions | decision trees |
| Why do flexible models overfit? | the bias–variance decomposition |
| One tree is unstable | bagging and random forests |
| Averaging does not fix bias | gradient boosting |
| Features must be designed by hand | neural networks, backpropagation, training deep networks |
| There are no labels at all | clustering, mixtures and EM, dimensionality reduction, anomaly detection |
| The output is an order, not a label | learning to rank, recommender systems |
The notes are grouped into seven sections: the learning problem (this note and the next three), linear regression, linear classification, trees and ensembles, neural networks, learning without labels, and problems beyond classification. After the course, a reader should be able to turn a business question into a supervised problem with a target, features and a metric; build a validation scheme that matches deployment and catches leakage; fit and debug linear models, trees and boosting and derive their gradients; choose between boosting and a neural network for a given dataset; and read scikit-learn or PyTorch training code knowing what each line does.
What changed#
| A program written by hand | A model learned from examples | |
|---|---|---|
| The rule | written by a person who understands the process | chosen from a family by minimizing |
| What it needs | knowledge of the mechanism | examples from the distribution of future cases |
| What “correct” means | passes the specified cases | small risk on new cases |
| How it is checked | tests of the specification | loss on held-out data, with an error bar |
| Typical failure | a bug | overfitting, leakage, a change of the distribution |
Limitations#
- New data must look like old data. Every guarantee is about pairs drawn from the same . A new market, a crisis, or a policy that reacts to the model’s own predictions changes , and the validated risk no longer applies.
- The loss is a choice. A model minimizes the loss it is given; on a problem with 1% fraud, never predicting fraud already scores 99% accuracy, so accuracy can rank a useless model above a useful one.
- Prediction is not causation. A model of says what tends to be when is observed, not what becomes when is changed: a churn model ranks the customers likely to leave, but does not say which offer would keep them.
- Labels cost. Supervised learning needs a target for every training example, and collecting targets is often the most expensive part of a project.
What you should remember#
- A supervised problem is a table of features and targets , a family of models and a loss; training minimizes the empirical risk .
- What matters is the risk on new data; for a model fixed in advance and a loss with finite variance, estimates it with error of order .
- A trained model’s training error is optimistic: already for a fitted constant, against a risk of .
- The best possible predictor is for squared loss and the most probable class for 0–1 loss; their risk is the irreducible error and the Bayes error.
- A loss decides the best prediction whatever the noise: the mean for squared error, the median for absolute error, the most probable class for 0–1. Some common losses also read as negative log-likelihoods (squared error of Gaussian noise, cross-entropy of a categorical model); not every useful loss does.
- Richer families lower the training error steadily; the error on new data falls and then rises. Only held-out data measure it.
Code#
The fitted constant’s optimism by simulation, and the polynomial errors of the figure exactly and by simulation:
import numpy as np
from numpy.polynomial import legendre as L
rng = np.random.default_rng(0)
# a constant prediction fitted to n = 4 values: its training error against its risk on new data (sigma^2 = 1)
n, sims = 4, 200_000
y = rng.normal(size=(sims, n))
c = y.mean(axis=1) # the fitted constant, y-bar
train = ((y - c[:, None]) ** 2).mean(axis=1) # empirical risk of y-bar on its own sample
risk = 1 + c ** 2 # E(Y - c)^2 for a new Y ~ N(0, 1)
print(round(float(train.mean()), 3), round(float(risk.mean()), 3)) # 0.751 1.25: (n-1)/n and (n+1)/n
print(np.mean([3, 5, 4, 8]), np.mean((np.array([3, 5, 4, 8]) - 5) ** 2)) # 5.0 3.5: the toy example
# polynomials fitted to 12 equally spaced points of sin(2 pi x) + N(0, 0.3^2): exact expected errors
m, s2, n = (lambda x: np.sin(2 * np.pi * x)), 0.09, 12
x = np.linspace(0, 1, n)
gx, gw = L.leggauss(200); gx, gw = (gx + 1) / 2, gw / 2 # quadrature on [0, 1] for a new X ~ U(0, 1)
def expected_errors(deg):
Phi = L.legvander(2 * x - 1, deg)
A = np.linalg.pinv(Phi) # y -> coefficients
G = L.legvander(2 * gx - 1, deg) @ A # y -> predictions on the quadrature grid
train = (((m(x) - Phi @ A @ m(x)) ** 2).sum() + s2 * (n - deg - 1)) / n
test = s2 + gw @ (G @ m(x) - m(gx)) ** 2 + s2 * gw @ (G ** 2).sum(axis=1)
return train, test
for deg in (0, 1, 3, 5, 9, 10, 11):
print(deg, *np.round(expected_errors(deg), 3))
# 0 0.541 0.598 | 1 0.329 0.318 | 3 0.067 0.121 | 5 0.045 0.128 | 9 0.015 0.208 | 10 0.008 0.421 | 11 0.0 3.158
# the same numbers by simulation for degree 3: 20,000 training sets, each scored on 1,000 fresh points
P = L.legvander(2 * x - 1, 3)
Y = m(x) + 0.3 * rng.normal(size=(20_000, n))
coef = np.linalg.lstsq(P, Y.T, rcond=None)[0]
xt = rng.random(1000)
Yt = m(xt) + 0.3 * rng.normal(size=(20_000, 1000))
print(round(float(((Y - (P @ coef).T) ** 2).mean()), 3), round(float(((Yt - (L.legvander(2 * xt - 1, 3) @ coef).T) ** 2).mean()), 3))
# 0.067 0.122
Next#
The setup assumed that every object arrives as a vector of numbers and that the training rows are independent draws from the distribution the model will meet. Real data arrive as tables with categories, missing values, dates and free text, and the easiest way to break the second assumption is to let information about the target slip into the features. The next note turns raw columns into features and shows how leakage makes a useless model look excellent.
References#
- E. Sokolov et al. Машинное обучение (HSE), lecture notes, lecture 1: introduction.
- T. Hastie, R. Tibshirani, J. Friedman. The Elements of Statistical Learning, 2nd ed. Springer, 2009. Chapter 2 (statistical decision theory, Section 2.4), Section 7.2 (model complexity and test error), Section 7.4 (optimism of the training error).
- S. Shalev-Shwartz, S. Ben-David. Understanding Machine Learning: From Theory to Algorithms. Cambridge University Press, 2014. Chapter 2 (empirical risk minimization and overfitting), Chapter 5 (the bias–complexity trade-off).
- C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006. Section 1.1 (polynomial curve fitting).
- L. Grinsztajn, E. Oyallon, G. Varoquaux. Why do tree-based models still outperform deep learning on tabular data? NeurIPS Datasets and Benchmarks Track, 2022.