Course contents 01 / 27

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 x∈Xx \in \mathcal X, usually Rd\mathbb R^d; its target is y∈Yy \in \mathcal Y. The pairs come from an unknown distribution PP on X×Y\mathcal X \times \mathcal Y, and the training set S={(x1,y1),…,(xn,yn)}S = \{(x_1, y_1), \dots, (x_n, y_n)\} holds nn independent draws from it. A model is a function ff from X\mathcal X to predictions, chosen from a family F\mathcal F; the trained model is f^\hat f. A loss L(y,y^)≥0L(y, \hat y) \ge 0 is the cost of predicting y^\hat y when the truth is yy.

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 f^\hat f 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 L(y,y^)=(y−y^)2L(y, \hat y) = (y - \hat y)^2, and the empirical risk of a model is its average loss on the training set:

R^n(f)=1n∑i=1nL(yi,f(xi)).\hat R_n(f) = \frac1n \sum_{i=1}^n L\big(y_i, f(x_i)\big) .

Most training procedures, from least squares to gradient boosting, minimize it over a family of models: f^=arg⁡min⁡f∈FR^n(f)\hat f = \arg\min_{f \in \mathcal F} \hat R_n(f), empirical risk minimization. What we care about, though, is the loss on the next object, the risk

R(f)=E(X,Y)∼P L(Y,f(X)),R(f) = E_{(X, Y) \sim P}\, L\big(Y, f(X)\big) ,

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 PP, the estimate converges to the risk by the law of large numbers. If the loss also has a finite variance σL2\sigma_L^2, the standard error is σL/n\sigma_L/\sqrt n, of order 1/n1/\sqrt n, 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 nn.

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 cc. Four flats cost 3, 5, 4 and 8 (millions). The squared loss is minimized by the mean, c=yˉ=5c = \bar y = 5, and its empirical risk is 14(4+0+1+9)=3.5\frac14(4 + 0 + 1 + 9) = 3.5. Is 3.5 the risk of predicting 5? Suppose the prices have mean μ\mu and variance σ2\sigma^2. On a new flat, the risk of the constant yˉ\bar y is E(Y−yˉ)2=σ2+(yˉ−μ)2E(Y - \bar y)^2 = \sigma^2 + (\bar y - \mu)^2, whose average over training sets is σ2+σ2/n\sigma^2 + \sigma^2/n. The empirical risk is 1n∑i(yi−yˉ)2\frac1n \sum_i (y_i - \bar y)^2, whose average is n−1nσ2\frac{n-1}{n}\sigma^2, the biased variance estimate of the estimators note. With n=4n = 4:

E R^n(yˉ)=0.75 σ2,E R(yˉ)=1.25 σ2.E\,\hat R_n(\bar y) = 0.75\,\sigma^2, \qquad E\,R(\bar y) = 1.25\,\sigma^2 .

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 2σ2/n2\sigma^2/n. 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 PP? For the squared loss, the answer is the conditional mean m(x)=E[Y∣X=x]m(x) = E[Y \mid X = x], the best predictor: for any ff, R(f)=R(m)+E (m(X)−f(X))2R(f) = R(m) + E\,(m(X) - f(X))^2. Its own risk, R(m)=EVar⁡(Y∣X)R(m) = E\operatorname{Var}(Y \mid X), 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):

LossL(y,y^)L(y, \hat y)Best prediction at xxLikelihood reading (optional)
Squared(y−y^)2(y - \hat y)^2mean of Y∣X=xY \mid X = xGaussian
Absolute∣y−y^∣\lvert y - \hat y \rvertmedian of Y∣X=xY \mid X = xLaplace
0–1[y≠y^][y \ne \hat y]most probable class
Cross-entropy−log⁡q(y∣x)-\log q(y \mid x)q(⋅∣x)=P(Y=⋅∣X=x)q(\cdot \mid x) = P(Y = \cdot \mid X = x)categorical

The third column needs no assumption about the noise. The conditional mean minimizes the expected squared loss for every distribution with E Y2<∞E\,Y^2 < \infty, Gaussian or not, and the conditional median minimizes the expected absolute loss whenever E∣Y∣<∞E\lvert Y \rvert < \infty. The loss decides which summary of Y∣X=xY \mid X = x 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 R∗=E[1−max⁡kP(Y=k∣X)]R^* = E\big[1 - \max_k P(Y = k \mid X)\big] is the Bayes error.

A trained model falls short of the best in two ways. Writing R∗R^* more generally for the smallest possible risk under the chosen loss (R(m)R(m) for the squared loss, the Bayes error for the 0–1 loss) and fF∗f^*_{\mathcal F} for the best member of the family,

R(f^)−R∗=R(f^)−R(fF∗)⏟estimation error+R(fF∗)−R∗⏟approximation error.R(\hat f) - R^* = \underbrace{R(\hat f) - R(f^*_{\mathcal F})}_{\text{estimation error}} + \underbrace{R(f^*_{\mathcal F}) - R^*}_{\text{approximation error}} .

A family that is too simple cannot approximate mm; a family that is too rich for nn 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#

TaskTargetExamplesTypical loss or metric
Regressiona numberprice, demand, delivery timesquared or absolute error
Binary classificationone of two classesdefault, churn, fraudcross-entropy; precision, recall, ROC-AUC
Multiclass, multilabelone of KK classes, or a set of labelstopic of an article, tagssoftmax cross-entropy
Rankingan order of items for a querysearch results, recommendationspairwise and listwise losses, NDCG
Clusteringnonecustomer segmentswithin-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 yiy_i when asked about xix_i 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 0.090.09 (the same setup as the bias–variance example); both curves are exact expected values over the noise:

Figure

Training error falls, error on new data does not

Training error and error on new data against the degreeThe blue training-error curve falls steadily to zero; the red curve of the error on new data falls until degree 3 and then rises, leaving the top of the plot at degree 11.00.20.40.601234567891011degree of the polynomialnoise 0.093.16new datatrainingsmallest at degree 3: 0.1210 at degree 11Training error and error on new data against the degreeThe blue training-error curve falls steadily to zero; the red curve of the error on new data falls until degree 3 and then rises, leaving the top of the plot at degree 11.00.20.40.6036911degree of the polynomialnoise 0.093.16new datatrainingsmallest: 0.1210 at degree 11
Polynomials of degree 0–11 fitted by least squares to 12 equally spaced points y = sin 2πx + noise (σ = 0.3). Both curves are exact expectations over the noise. The training error falls with every coefficient and is 0 at degree 11, where the polynomial passes through all the points. The error at a new point X ~ U(0, 1) is smallest at degree 3 (0.121), and is 0.208 at degree 9, 0.421 at degree 10 and 3.16 at degree 11. The dashed line is the noise, 0.09, below which no model's error on new data can go.

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 σ2(n−p)/n\sigma^2 (n - p)/n for p=degree+1p = \text{degree} + 1 coefficients: each coefficient absorbs one direction of the noise, 0.09/12=0.00750.09/12 = 0.0075 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 KK candidates chosen on the same validation set, the bound pays ln⁡K\ln K 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 toolWhat it becomes hereWhere NLP and Generative Models use it
Expectationrisk 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 bootstrapgeneralization, validation and its error bars, selection bias, metricsevaluation and leakage in NLP
Conditional expectationthe target of least squares and of nearest neighbours; the bias–variance split
Maximum likelihoodsquared error read as a Gaussian likelihood; cross-entropy in logistic regression and the softmax; the EM algorithmtext classifiers, language models, cross-entropy and KL
Bayesian inferenceridge and lasso as priors (regularization); smoothed target encodingweight decay in fine-tuning
The multivariate GaussianPCA, Gaussian mixtures, the geometry of nearest neighbours in high dimensionautoencoders 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.

Figure

The course as a bridge: from probability to NLP and generative models

Three columns: probability theory, machine learning, NLP and generative modelsLeft, six concepts of probability theory; middle, eight topics of machine learning; right, eight uses in NLP and generative models. Lines join each concept to the topic built on it and each topic to its uses.Probability TheoryMachine LearningNLP · Generative ModelsExpectationrisk is an expected lossConcentration, LLN, CLTwhy an average generalizesConditional expectationthe best possible predictorMaximum likelihoodlosses as log-likelihoodsBayesian inferencepriors become penaltiesMultivariate Gaussiangeometry of high dimensionRisk and empirical riskthis noteValidation and metricserror bars, selection biasGradient descent, backpropfitting by small stepsLeast squaresand the bias–variance splitLogistic regressionprobabilities and softmaxRegularizationridge, lasso, weight decayNearest neighboursprediction by similarityPCA, mixtures and EMstructure without labelsEvaluation and leakagesplits and errors in NLPTraining RNNs, Transformersgradients through timeText classifierslinear models on countsLanguage modelsnext-token cross-entropyCross-entropy and KLtraining generative modelsFine-tuningweight decay on a big modelSemantic searchnearest embeddingsAutoencoders and VAEsPCA and EM, made deepThree columns: probability theory, machine learning, NLP and generative modelsLeft, six concepts of probability theory; middle, eight topics of machine learning; right, eight uses in NLP and generative models. Lines join each concept to the topic built on it and each topic to its uses.ProbabilityMLNLP · GMExpectationLLN, CLT,concentrationConditionalexpectationMaximumlikelihoodBayesianinferenceMultivariateGaussianEmpiricalriskValidation,metricsGradients,backpropLeastsquaresLogistic,softmaxRidge, lasso,weight decayNearestneighboursPCA,mixtures, EMEvaluation,leakageTrainingTransformersTextclassifiersLanguagemodelsCross-entropyand KLFine-tuningSemanticsearchAutoencoders,VAEs
Six tools of probability theory (left) feed eight groups of notes in this course (middle), and these feed eight places where the NLP course (green) and the Generative Models course (red) use them. Each of the 20 lines is one dependency; the table above names the notes behind each.

The course map#

Each step either answers a limitation of the one before it or, in the last rows, changes the task:

FailureAnswer
Memorizing the training set does not generalizerisk and validation: this note, data, features and leakage, validation
Similar inputs should get similar outputs, but similarity in many dimensions is nearly meaninglessnearest neighbours and the curse of dimensionality
A neighbour vote has no global structureleast squares
The closed form is too expensive or does not existgradient descent, stochastic and adaptive optimizers
With many features the fit chases noiseregularization
Least squares on class labels lets easy points move the boundarylinear classifiers and surrogate losses
We need probabilities, not only a side of a linelogistic regression
Accuracy hides the errors that matterclassification metrics
The best separating line is not unique, and lines are too simplesupport vector machines, kernels
More than two classes, or several labels at oncemulticlass and multilabel classification
A line cannot express interactionsdecision trees
Why do flexible models overfit?the bias–variance decomposition
One tree is unstablebagging and random forests
Averaging does not fix biasgradient boosting
Features must be designed by handneural networks, backpropagation, training deep networks
There are no labels at allclustering, mixtures and EM, dimensionality reduction, anomaly detection
The output is an order, not a labellearning 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 handA model learned from examples
The rulewritten by a person who understands the processchosen from a family F\mathcal F by minimizing R^n\hat R_n
What it needsknowledge of the mechanismexamples from the distribution of future cases
What “correct” meanspasses the specified casessmall risk R(f)R(f) on new cases
How it is checkedtests of the specificationloss on held-out data, with an error bar
Typical failurea bugoverfitting, leakage, a change of the distribution

Limitations#

  • New data must look like old data. Every guarantee is about pairs drawn from the same PP. A new market, a crisis, or a policy that reacts to the model’s own predictions changes PP, 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 P(Y∣X)P(Y \mid X) says what YY tends to be when XX is observed, not what YY becomes when XX 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 xx and targets yy, a family of models and a loss; training minimizes the empirical risk R^n(f)=1n∑iL(yi,f(xi))\hat R_n(f) = \frac1n\sum_i L(y_i, f(x_i)).
  • What matters is the risk R(f)=E L(Y,f(X))R(f) = E\,L(Y, f(X)) on new data; for a model fixed in advance and a loss with finite variance, R^n\hat R_n estimates it with error of order 1/n1/\sqrt n.
  • A trained model’s training error is optimistic: already for a fitted constant, ER^n=n−1nσ2E\hat R_n = \frac{n-1}{n}\sigma^2 against a risk of n+1nσ2\frac{n+1}{n}\sigma^2.
  • The best possible predictor is E[Y∣X]E[Y \mid X] 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#