














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















This chapter discusses tools to analyze and understand the generaliza- tion of machine learning models, i.e, their performances on unseen test examples. Recall that for supervised learning problems, given a train- ing dataset {(x(i), y(i))}ni=1, we typically learn a model hθ by minimizing a loss/cost function J(θ), which encourages hθ to fit the data. E.g., when the loss function is the least square loss (aka mean squared error), we have J(θ) = (^1) n
∑n i=1(y
(i) (^) − hθ(x(i))) (^2). This loss function for training purposes is
oftentimes referred to as the training loss/error/cost. However, minimizing the training loss is not our ultimate goal—it is merely our approach towards the goal of learning a predictive model. The most important evaluation metric of a model is the loss on unseen test exam- ples, which is oftentimes referred to as the test error. Formally, we sample a test example (x, y) from the so-called test distribution D, and measure the model’s error on it, by, e.g., the mean squared error, (hθ(x) − y)^2. The ex- pected loss/error over the randomness of the test example is called the test loss/error,^1
L(θ) = E(x,y)∼D[(y − hθ(x))^2 ] (8.1)
Note that the measurement of the error involves computing the expectation, and in practice, it can be approximated by the average error on many sampled test examples, which are referred to as the test dataset. Note that the key difference here between training and test datasets is that the test examples
(^1) In theoretical and statistical literature, we oftentimes call the uniform distribution
over the training set {(x(i), y(i))}ni=1, denoted by D̂ , an empirical distribution, and call D the population distribution. Partly because of this, the training loss is also referred to as the empirical loss/risk/error, and the test loss is also referred to as the population loss/risk/error.
are unseen, in the sense that the training procedure has not used the test examples. In classical statistical learning settings, the training examples are also drawn from the same distribution as the test distribution D, but still the test examples are unseen by the learning procedure whereas the training examples are seen.^2 Because of this key difference between training and test datasets, even if they are both drawn from the same distribution D, the test error is not necessarily always close to the training error.^3 As a result, successfully min- imizing the training error may not always lead to a small test error. We typically say the model overfits the data if the model predicts accurately on the training dataset but doesn’t generalize well to other test examples, that is, if the training error is small but the test error is large. We say the model underfits the data if the training error is relatively large^4 (and in this case, typically the test error is also relatively large.) This chapter studies how the test error is influenced by the learning pro- cedure, especially the choice of model parameterizations. We will decompose the test error into “bias” and “variance” terms and study how each of them is affected by the choice of model parameterizations and their tradeoffs. Using the bias-variance tradeoff, we will discuss when overfitting and underfitting will occur and be avoided. We will also discuss the double descent phe- nomenon in Section 8.2 and some classical theoretical results in Section 8.3. (^2) These days, researchers have increasingly been more interested in the setting with
“domain shift”, that is, the training distribution and test distribution are different. (^3) the difference between test error and training error is often referred to as the gener-
alization gap. The term generalization error in some literature means the test error, and in some other literature means the generalization gap. (^4) e.g., larger than the intrinsic noise level of the data in regression problems.
0.0 0.2 0.4 0.6 0.8 1. x
y
training data best fit linear model
0.0 0.2 0.4 0.6 0.8 1. x
y
test data best fit linear model
Figure 8.2: The best fit linear model has large training and test errors.
The issue cannot be mitigated with more training examples—even with a very large amount of, or even infinite training examples, the best fitted linear model is still inaccurate and fails to capture the structure of the data (Figure 8.3). Even if the noise is not present in the training data, the issue still occurs (Figure 8.4). Therefore, the fundamental bottleneck here is the linear model family’s inability to capture the structure in the data—linear models cannot represent the true quadratic function h?—, but not the lack of the data. Informally, we define the bias of a model to be the test error even if we were to fit it to a very (say, infinitely) large training dataset. Thus, in this case, the linear model suffers from large bias, and underfits (i.e., fails to capture structure exhibited by) the data.
0.0 0.2 0.4 0.6 0.8 1. x
y
fitting linear models on a large dataset training data ground truth h * best fit linear model
Figure 8.3: The best fit linear model on a much larger dataset still has a large training error.
0.0 0.2 0.4 0.6 0.8 1. x
y
fitting linear models on a noiseless dataset training data ground truth h * best fit linear model
Figure 8.4: The best fit linear model on a noiseless dataset also has a large training/test error.
Next, we fit a 5th-degree polynomial to the data. Figure 8.5 shows that it fails to learn a good model either. However, the failure pattern is different from the linear model case. Specifically, even though the learnt 5th-degree
polynomial did a very good job predicting y(i)’s from x(i)’s for training ex- amples, it does not work well on test examples (Figure 8.5). In other words, the model learnt from the training set does not generalize well to other test examples—the test error is high. Contrary to the behavior of linear models, the bias of the 5-th degree polynomials is small—if we were to fit a 5-th de- gree polynomial to an extremely large dataset, the resulting model would be close to a quadratic function and be accurate (Figure 8.6). This is because the family of 5-th degree polynomials contains all the quadratic functions (setting θ 5 = θ 4 = θ 3 = 0 results in a quadratic function), and, therefore, 5-th degree polynomials are in principle capable of capturing the structure of the data.
0.0 0.2 0.4 0.6 0.8 1. x
y
training data best fit 5-th degree model
0.0 0.2 0.4 0.6 0.8 1. x
y
test data ground truth h * best fit 5-th degree model
Figure 8.5: The best-fit fifth-degree polynomial has zero training error, but still has a large test error and does not recover the ground truth. This is a classic situation of overfitting.
0.0 0.2 0.4 0.6 0.8 1. x
y
training data best fit 5-th degree model ground truth h *
fitting 5-th degree model on large dataset
Figure 8.6: The best fit 5-th degree polynomial on a huge dataset nearly recovers the ground-truth—suggesting that the culprit in Figure 8.5 is the variance (or lack of data) but not bias.
The failure of fitting 5-th degree polynomials can be captured by another
Model Complexity
Error
Bias^2
Variance
Optimal Tradeoff Test Error (= Bias^2 +Variance)
Figure 8.8: An illustration of the typical bias-variance tradeoff.
As we will see formally in Section 8.1.1, the test error can be decomposed as a summation of bias and variance. This means that the test error will have a convex curve as the model complexity increases, and in practice we should tune the model complexity to achieve the best tradeoff. For instance, in the example above, fitting a quadratic function does better than either of the extremes of a first or a 5-th degree polynomial, as shown in Figure 8.9.
0.0 0.2 0.4 0.6 0.8 1. x
y
training data best fit quadratic model
0.0 0.2 0.4 0.6 0.8 1. x
y
test data best fit quadratic model ground truth h *
Figure 8.9: Best fit quadratic model has small training and test error because quadratic model achieves a better tradeoff.
Interestingly, the bias-variance tradeoff curves or the test error curves do not universally follow the shape in Figure 8.8, at least not universally when the model complexity is simply measured by the number of parameters. (We will discuss the so-called double descent phenomenon in Section 8.2.) Nevertheless, the principle of bias-variance tradeoff is perhaps still the first resort when analyzing and predicting the behavior of test errors.
To formally state the bias-variance tradeoff for regression problems, we con- sider the following setup (which is an extension of the beginning paragraph of Section 8.1).
MSE(x) = ES,ξ[(y − hS (x))^2 ] (8.2)
We will decompose the MSE into a bias and variance term. We start by stating a following simple mathematical tool that will be used twice below.
Claim 8.1.1.: Suppose A and B are two independent real random variables and E[A] = 0. Then, E[(A + B)^2 ] = E[A^2 ] + E[B^2 ]. As a corollary, because a random variable A is independent with a con- stant c, when E[A] = 0, we have E[(A + c)^2 ] = E[A^2 ] + c^2.
The proof of the claim follows from expanding the square: E[(A + B)^2 ] = E[A^2 ] + E[B^2 ] + 2E[AB] = E[A^2 ] + E[B^2 ]. Here we used the independence to show that E[AB] = E[A]E[B] = 0. Using Claim 8.1.1 with A = ξ and B = h?(x) − ˆhS (x), we have
MSE(x) = E[(y − hS (x))^2 ] = E[(ξ + (h?(x) − hS (x)))^2 ] (8.3) = E[ξ^2 ] + E[(h?(x) − hS (x))^2 ] (by Claim 8.1.1) = σ^2 + E[(h?(x) − hS (x))^2 ] (8.4)
Then, let’s define havg(x) = ES [hS (x)] as the “average model”—the model obtained by drawing an infinite number of datasets, training on them, and averaging their predictions on x. Note that havg is a hypothetical model for analytical purposes that cannot be obtained in reality (because we don’t
(^5) For simplicity, the test input x is considered to be fixed here, but the same conceptual
message holds when we average over the choice of x’s. (^6) The subscript under the expectation symbol is to emphasize the variables that are
considered as random by the expectation operation.
learning models including linear models and deep neural networks.^7 The conventional wisdom, as discussed in Section 8.1, is that as we increase the model complexity, the test error first decreases and then increases, as illus- trated in Figure 8.8. However, in many cases, we empirically observe that the test error can have a second descent—it first decreases, then increases to a peak around when the model size is large enough to fit all the training data very well, and then decreases again in the so-called overparameterized regime, where the number of parameters is larger than the number of data points. See Figure 8.10 for an illustration of the typical curves of test errors against model complexity (measured by the number of parameters). To some extent, the overparameterized regime with the second descent is considered as new to the machine learning community—partly because lightly-regularized, overparameterized models are only extensively used in the deep learning era. A practical implication of the phenomenon is that one should not hold back from scaling into and experimenting with over-parametrized models because the test error may well decrease again to a level even smaller than the previ- ous lowest point. Actually, in many cases, larger overparameterized models always lead to a better test performance (meaning there won’t be a second ascent after the second descent).
test error
typically when # parameters is sufficient to fit the data
classical regime: bias-variance tradeoff
modern regime: over-parameterization
Figure 8.10: A typical model-wise double descent phenomenon. As the num- ber of parameters increases, the test error first decreases when the number of parameters is smaller than the training data. Then in the overparameterized regime, the test error decreases again.
(^7) The discovery of the phenomenon perhaps dates back to Opper [1995, 2001], and has
been recently popularized by Belkin et al. [2020], Hastie et al. [2019], etc.
Sample-wise double descent. A priori, we would expect that more training examples always lead to smaller test errors—more samples give strictly more information for the algorithm to learn from. However, recent work [Nakkiran, 2019] observes that the test error is not monotonically de- creasing as we increase the sample size. Instead, as shown in Figure 8.11, the test error decreases, and then increases and peaks around when the number of examples (denoted by n) is similar to the number of parameters (denoted by d), and then decreases again. We refer to this as the sample-wise dou- ble descent phenomenon. To some extent, sample-wise double descent and model-wise double descent are essentially describing similar phenomena—the test error is peaked when n ≈ d.
Explanation and mitigation strategy. The sample-wise double descent, or, in particular, the peak of test error at n ≈ d, suggests that the existing training algorithms evaluated in these experiments are far from optimal when n ≈ d. We will be better off by tossing away some examples and run the algorithms with a smaller sample size to steer clear of the peak. In other words, in principle, there are other algorithms that can achieve smaller test error when n ≈ d, but the algorithms evaluated in these experiments fail to do so. The sub-optimality of the learning procedure appears to be the culprit of the peak in both sample-wise and model-wise double descent. Indeed, with an optimally-tuned regularization (which will be discussed more in Section 9), the test error in the n ≈ d regime can be dramatically improved, and the model-wise and sample-wise double descent are both mit- igated. See Figure 8.11. The intuition above only explains the peak in the model-wise and sample- wise double descent, but does not explain the second descent in the model- wise double descent—why overparameterized models are able to generalize so well. The theoretical understanding of overparameterized models is an ac- tive research area with many recent advances. A typical explanation is that the commonly-used optimizers such as gradient descent provide an implicit regularization effect (which will be discussed in more detail in Section 9.2). In other words, even in the overparameterized regime and with an unregular- ized loss function, the model is still implicitly regularized, and thus exhibits a better test performance than an arbitrary solution that fits the data. For example, for linear models, when n d, the gradient descent optimizer with zero initialization finds the minimum norm solution that fits the data (in- stead of an arbitrary solution that fits the data), and the minimum norm reg- ularizer turns out to be a sufficiently good for the overparameterized regime (but it’s not a good regularizer when n ≈ d, resulting in the peak of test
0.0 (^0 250) # (^) parameters 500 750 1000
test
err
or
test error vs. # params
(^00 200) # (^400) parameters 600 800 1000
10
20
30
40
norm
norm vs. # params
0.0 (^0 10) norm 20 30 40
test
err
or^ d=n
#^ parameters
test error vs. norm
0
200
400
600
800
1000
Figure 8.12: Left: The double descent phenomenon, where the number of pa- rameters is used as the model complexity. Middle: The norm of the learned model is peaked around n ≈ d. Right: The test error against the norm of the learnt model. The color bar indicate the number of parameters and the arrows indicates the direction of increasing model size. Their relationship are closer to the convention wisdom than to a double descent. Setup: We consider a linear regression with a fixed dataset of size n = 500. The input x is a random ReLU feature on Fashion-MNIST, and output y ∈ R^10 is the one-hot label. This is the same setting as in Section 5.2 of Nakkiran et al. [2020].
In this set of notes, we begin our foray into learning theory. Apart from being interesting and enlightening in its own right, this discussion will also help us hone our intuitions and derive rules of thumb about how to best apply learning algorithms in different settings. We will also seek to answer a few questions: First, can we make formal the bias/variance tradeoff that was just discussed? This will also eventually lead us to talk about model selection methods, which can, for instance, automatically decide what order polynomial to fit to a training set. Second, in machine learning it’s really generalization error that we care about, but most learning algorithms fit their models to the training set. Why should doing well on the training set tell us anything about generalization error? Specifically, can we relate error on the training set to generalization error? Third and finally, are there conditions under which we can actually prove that learning algorithms will work well? We start with two simple but very useful lemmas.
Lemma. (The union bound). Let A 1 , A 2 ,... , Ak be k different events (that may not be independent). Then
P (A 1 ∪ · · · ∪ Ak) ≤ P (A 1 ) +... + P (Ak).
In probability theory, the union bound is usually stated as an axiom (and thus we won’t try to prove it), but it also makes intuitive sense: The probability of any one of k events happening is at most the sum of the probabilities of the k different events.
Lemma. (Hoeffding inequality) Let Z 1 ,... , Zn be n independent and iden- tically distributed (iid) random variables drawn from a Bernoulli(φ) distri- bution. I.e., P (Zi = 1) = φ, and P (Zi = 0) = 1 − φ. Let φˆ = (1/n)
∑n i=1 Zi be the mean of these random variables, and let any γ > 0 be fixed. Then
P (|φ − φˆ| > γ) ≤ 2 exp(− 2 γ^2 n)
This lemma (which in learning theory is also called the Chernoff bound) says that if we take φˆ—the average of n Bernoulli(φ) random variables—to be our estimate of φ, then the probability of our being far from the true value is small, so long as n is large. Another way of saying this is that if you have a biased coin whose chance of landing on heads is φ, then if you toss it n times and calculate the fraction of times that it came up heads, that will be a good estimate of φ with high probability (if n is large).
In our study of learning theory, it will be useful to abstract away from the specific parameterization of hypotheses and from issues such as whether we’re using a linear classifier. We define the hypothesis class H used by a learning algorithm to be the set of all classifiers considered by it. For linear classification, H = {hθ : hθ(x) = 1{θT^ x ≥ 0 }, θ ∈ Rd+1} is thus the set of all classifiers over X (the domain of the inputs) where the decision boundary is linear. More broadly, if we were studying, say, neural networks, then we could let H be the set of all classifiers representable by some neural network architecture. Empirical risk minimization can now be thought of as a minimization over the class of functions H, in which the learning algorithm picks the hypothesis:
ˆh = arg min h∈H
ˆε(h)
Let’s start by considering a learning problem in which we have a finite hy- pothesis class H = {h 1 ,... , hk} consisting of k hypotheses. Thus, H is just a set of k functions mapping from X to { 0 , 1 }, and empirical risk minimization selects ˆh to be whichever of these k functions has the smallest training error. We would like to give guarantees on the generalization error of ˆh. Our strategy for doing so will be in two parts: First, we will show that ˆε(h) is a reliable estimate of ε(h) for all h. Second, we will show that this implies an upper-bound on the generalization error of ˆh. Take any one, fixed, hi ∈ H. Consider a Bernoulli random variable Z whose distribution is defined as follows. We’re going to sample (x, y) ∼ D. Then, we set Z = 1{hi(x) 6 = y}. I.e., we’re going to draw one example, and let Z indicate whether hi misclassifies it. Similarly, we also define Zj = 1 {hi(x(j)) 6 = y(j)}. Since our training set was drawn iid from D, Z and the Zj ’s have the same distribution. We see that the misclassification probability on a randomly drawn example—that is, ε(h)—is exactly the expected value of Z (and Zj ). More- over, the training error can be written
εˆ(hi) =
n
∑^ n
j=
Zj.
Thus, ˆε(hi) is exactly the mean of the n random variables Zj that are drawn iid from a Bernoulli distribution with mean ε(hi). Hence, we can apply the Hoeffding inequality, and obtain
P (|ε(hi) − εˆ(hi)| > γ) ≤ 2 exp(− 2 γ^2 n).
This shows that, for our particular hi, training error will be close to generalization error with high probability, assuming n is large. But we don’t just want to guarantee that ε(hi) will be close to ˆε(hi) (with high probability) for just only one particular hi. We want to prove that this will be true simultaneously for all h ∈ H. To do so, let Ai denote the event that |ε(hi) − ε ˆ(hi)| > γ. We’ve already shown that, for any particular Ai, it holds true that P (Ai) ≤ 2 exp(− 2 γ^2 n). Thus, using the union bound, we have that
P (∃ h ∈ H.|ε(hi) − εˆ(hi)| > γ) = P (A 1 ∪ · · · ∪ Ak)
≤
∑^ k
i=
P (Ai)
∑^ k
i=
2 exp(− 2 γ^2 n)
= 2 k exp(− 2 γ^2 n)
If we subtract both sides from 1, we find that
P (¬∃ h ∈ H.|ε(hi) − εˆ(hi)| > γ) = P (∀h ∈ H.|ε(hi) − εˆ(hi)| ≤ γ) ≥ 1 − 2 k exp(− 2 γ^2 n)
(The “¬” symbol means “not.”) So, with probability at least 1 − 2 k exp(− 2 γ^2 n), we have that ε(h) will be within γ of ˆε(h) for all h ∈ H. This is called a uniform convergence result, because this is a bound that holds simultaneously for all (as opposed to just one) h ∈ H. In the discussion above, what we did was, for particular values of n and γ, give a bound on the probability that for some h ∈ H, |ε(h) − εˆ(h)| > γ. There are three quantities of interest here: n, γ, and the probability of error; we can bound either one in terms of the other two. For instance, we can ask the following question: Given γ and some δ > 0, how large must n be before we can guarantee that with probability at least 1 − δ, training error will be within γ of generalization error? By setting δ = 2k exp(− 2 γ^2 n) and solving for n, [you should convince yourself this is the right thing to do!], we find that if
n ≥
2 γ^2
log
2 k δ
then with probability at least 1 − δ, we have that |ε(h) − εˆ(h)| ≤ γ for all h ∈ H. (Equivalently, this shows that the probability that |ε(h) − εˆ(h)| > γ
This is proved by letting γ equal the
· term, using our previous argu- ment that uniform convergence occurs with probability at least 1 − δ, and then noting that uniform convergence implies ε(h) is at most 2γ higher than ε(h∗) = minh∈H ε(h) (as we showed previously). This also quantifies what we were saying previously saying about the bias/variance tradeoff in model selection. Specifically, suppose we have some hypothesis class H, and are considering switching to some much larger hy- pothesis class H′^ ⊇ H. If we switch to H′, then the first term minh ε(h) can only decrease (since we’d then be taking a min over a larger set of func- tions). Hence, by learning using a larger hypothesis class, our “bias” can only decrease. However, if k increases, then the second 2
· term would also increase. This increase corresponds to our “variance” increasing when we use a larger hypothesis class. By holding γ and δ fixed and solving for n like we did before, we can also obtain the following sample complexity bound:
Corollary. Let |H| = k, and let any δ, γ be fixed. Then for ε(ˆh) ≤ minh∈H ε(h) + 2γ to hold with probability at least 1 − δ, it suffices that
n ≥
2 γ^2
log
2 k δ
= O
γ^2
log
k δ
We have proved some useful theorems for the case of finite hypothesis classes. But many hypothesis classes, including any parameterized by real numbers (as in linear classification) actually contain an infinite number of functions. Can we prove similar results for this setting? Let’s start by going through something that is not the “right” argument. Better and more general arguments exist, but this will be useful for honing our intuitions about the domain. Suppose we have an H that is parameterized by d real numbers. Since we are using a computer to represent real numbers, and IEEE double-precision floating point (double’s in C) uses 64 bits to represent a floating point num- ber, this means that our learning algorithm, assuming we’re using double- precision floating point, is parameterized by 64d bits. Thus, our hypothesis class really consists of at most k = 2^64 d^ different hypotheses. From the Corol- lary at the end of the previous section, we therefore find that, to guarantee ε(ˆh) ≤ ε(h∗) + 2γ, with to hold with probability at least 1 − δ, it suffices that
n ≥ O
1 γ^2 log^
264 d δ
d γ^2 log^
1 δ
= Oγ,δ(d). (The γ, δ subscripts indicate
that the last big-O is hiding constants that may depend on γ and δ.) Thus, the number of training examples needed is at most linear in the parameters of the model. The fact that we relied on 64-bit floating point makes this argument not entirely satisfying, but the conclusion is nonetheless roughly correct: If what we try to do is minimize training error, then in order to learn “well” using a hypothesis class that has d parameters, generally we’re going to need on the order of a linear number of training examples in d. (At this point, it’s worth noting that these results were proved for an al- gorithm that uses empirical risk minimization. Thus, while the linear depen- dence of sample complexity on d does generally hold for most discriminative learning algorithms that try to minimize training error or some approxima- tion to training error, these conclusions do not always apply as readily to discriminative learning algorithms. Giving good theoretical guarantees on many non-ERM learning algorithms is still an area of active research.) The other part of our previous argument that’s slightly unsatisfying is that it relies on the parameterization of H. Intuitively, this doesn’t seem like it should matter: We had written the class of linear classifiers as hθ(x) = 1 {θ 0 + θ 1 x 1 + · · · θdxd ≥ 0 }, with n + 1 parameters θ 0 ,... , θd. But it could also be written hu,v(x) = 1{(u^20 − v^20 ) + (u^21 − v^21 )x 1 + · · · (u^2 d − v^2 d)xd ≥ 0 } with 2d + 2 parameters ui, vi. Yet, both of these are just defining the same H: The set of linear classifiers in d dimensions. To derive a more satisfying argument, let’s define a few more things. Given a set S = {x(i),... , x(D)} (no relation to the training set) of points x(i)^ ∈ X , we say that H shatters S if H can realize any labeling on S. I.e., if for any set of labels {y(1),... , y(D)}, there exists some h ∈ H so that h(x(i)) = y(i)^ for all i = 1,... D. Given a hypothesis class H, we then define its Vapnik-Chervonenkis dimension, written VC(H), to be the size of the largest set that is shattered by H. (If H can shatter arbitrarily large sets, then VC(H) = ∞.) For instance, consider the following set of three points: