🌏 中文版
Video status: Videos included. This article is written from the official MOOC handout 15_handout.pdf and transcripts of this lecture's four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/22 to 22/22), and wherever the text says "in the lecture" it is paraphrasing what is said in the video.
This is Foundations Lecture 15 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. The Roadmap on page 1 of the handout divides Foundations into four questions, and this lecture belongs to the fourth, How Can Machines Learn Better?. The previous lecture, Foundations Lecture 14: Regularization, takes up a single line on that page: it minimizes augmented error, where the added regularizer effectively limits model complexity. That lecture left one question unanswered: how large the regularization strength should be depends on how much noise there is, and the amount of noise is not known in advance. Validation, the subject of this lecture, is the tool for making that kind of choice.
There are three prerequisites: the finite-hypothesis Hoeffding inequality from Foundations Lecture 4, linear regression and the learning curve from Foundations Lecture 9, and the polynomial transform from Foundations Lecture 12. Linearity of expectation is used once; the check made up for this article also uses the fact that when two independent random variables are subtracted, their variances add.
After reading, you should be able to explain why neither nor can be used to select a model, write down the full validation procedure and its guarantee, derive on your own why the expectation of leave-one-out equals , and compute , , and by hand for a small data set.
With so many combinations, how do you pick one: the model selection problem
(Video 00:18–01:14 reviews the previous lecture, 01:14–03:20 is the table of combinations, 03:20–06:17 defines the model selection problem, 06:17–09:57 discusses selecting by , 09:57–12:40 discusses selecting by , 12:40–15:06 leads to validation data, and Fun Time follows.)
So many choices that you have to multiply
Page 2 of the handout lays out what the previous fourteen lectures taught in one table. For binary classification alone there are six things to decide:
| What to decide | Options listed in the handout |
|---|---|
| Algorithm | PLA, pocket, linear regression, logistic regression |
| Number of iterations | 100, 1000, 10000 |
| Step size | 1, 0.01, 0.0001 |
| Feature transform | linear, quadratic, poly-10, Legendre-poly-10 |
| Regularizer | L2, L1, symmetry regularizer |
| Regularization strength | 0, 0.01, 1 |
The slide draws a multiplication sign between every two rows. This article multiplies it out: combinations, and every row could take more options. In the lecture, Lin explains that the more methods are taught, the more choices there are to make, and that these choices combine. Everyone has a favorite combination, just as everyone has a favorite food; but always using the same one will not do well on every data set. The bottom of the slide says the same thing: in addition to your favorite combination, you may need to try other combinations to get a good .
Stating the problem clearly
Page 3 gives the formal definition, called the model selection problem:
- Given: models , each with corresponding algorithm .
- Goal: select such that is of low .
- Difficulty: cannot be computed, because and are unknown. The handout adds: as always.
A "model" here means a hypothesis set together with the algorithm that runs on it, and every combination in the table above counts as one. The star on marks the one that gets selected; the example in the lecture is 100 combinations from which number 37 is selected, and for the rest of the lecture the selected model is called "number 37".
The handout calls this arguably the most important practical problem of ML. The reason given in the lecture is that in practice no single model and single algorithm works everywhere, so a choice has to be made.
Picking by eye does not work
The top of page 3 shows two figures and asks which one you prefer: on the left, is a straight line that misclassifies a few points; on the right, is a curved boundary that misclassifies none. The bottom of the slide asks and answers: select visually? No, remember Lecture 12?
The lecture repeats the two reasons from Foundations Lecture 12. First, once the data has many dimensions it cannot be drawn, so there is nothing to look at. Second, when you choose by eye, the powerful model in your head and its VC dimension have to be counted in the price too, and that may not be a good deal.
Picking by training error: you always get the most complex one
With only the data on hand, the most direct approach is to run every model once and pick the one with the smallest (page 4):
The handout points out two problems.
The first problem: the result is known in advance. The handout writes that is always more preferred over , and always more preferred over . The reason is that these pairs of options are nested. The hypothesis set of a higher-order transform contains the lower-order one, and looking for the minimum- hypothesis in the larger set can never do worse than looking in the smaller set. minimizes directly, while minimizes plus the regularizer, so the the latter reaches cannot be lower than the former's. The lecture puts it bluntly: select this way and there was no point in the previous lecture's regularization, since you would just drive to its minimum, which may put you in danger of overfitting.
The second problem: the complexity price goes up. This holds even when the candidates are not nested. Suppose there are only two models, where finds the minimum- hypothesis in and finds the minimum- hypothesis in , and you then pick whichever of the two has the smaller . Taking the smaller of the two sets' minima gives the minimum over their union:
So the whole "model selection + learning" procedure is really minimizing over and pays , and generalization may get worse. The analogy in the lecture is two classes of students: each algorithm picks the student with the smallest from its own class, and model selection then has the two top students compete, so overall you are picking the single best student from the two classes combined.
The handout's conclusion is selecting by is dangerous.
Six data points: whom does training error pick
The following is an example made up for this article, not from the slides, and the next three sections keep using it. The data is six one-dimensional regression examples, with squared error:
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | |
| 1 | 1 | 2.5 | 3 | 3 | 3 |
This article made up these six points from plus hand-picked noise , so the right answer is a straight line. Looking only at the values, which climb at first and then level off, it is easy to feel that a curve that bends is the right choice.
There are three candidate models, all trained with the least-squares method of Foundations Lecture 9: the constant , the line , and the quadratic . Each contains the one before it. Training on all six points gives:
| Model | trained on all six points | |
|---|---|---|
| Constant | ||
| Line | ||
| Quadratic |
The constant model's solution is the average of the six values, , and its is . The other two are solved by least squares; a short piece of Python near the end of the article recomputes them.
gets smaller from top to bottom, so selecting by picks the quadratic. You can predict this result without looking at the data, because the quadratic model contains the line and the line contains the constant; if the candidates also included a degree-five polynomial, it would pass through all six points with and be the one selected.
Picking by test error: a great guarantee, but the data is out of reach
Here is another idea (page 5): find a fresh test set that has never been used, see how the learned by each model does on it, and pick the one with the smallest .
Selecting this way comes with a theoretical guarantee. are already fixed before is seen, so this is choosing one of fixed hypotheses, and the finite-hypothesis Hoeffding inequality from Foundations Lecture 4 applies (the handout calls it finite-bin Hoeffding):
The handout's comment is yes! strong guarantee. The lecture uses 100 models as the example: take the , take the square root, and divide by the number of test examples as well, and the price is small, unlike , which pays the model complexity of the entire union. The section "Holding out K examples as a validation set" derives where this formula comes from.
The problem is where is. The handout writes your boss's safe; in the lecture, Lin says it is locked in the boss's cabinet. The analogy in the lecture is a teacher's exam: hand the exam out before the test for students to take home, and the scores no longer mean anything. If you really do get hold of the test data, it was most likely stolen, and the lecture calls that fooling yourself. The handout's conclusion is selecting by is infeasible and cheating.
The compromise: hold out a piece of the data on hand
Page 6 puts the two approaches side by side:
| Calculated from | |||
| Can you get it | On hand | In the boss's safe | On hand |
| Is it clean | Contaminated: has already used to select | Clean: never used for selection before | Clean, provided has never used |
"Contaminated" is the key word of this lecture. has already been used by every algorithm to decide , and using the same data again to choose among them is reusing it. is like a freshly written exam that measures ability cleanly.
The road in between is to hold out a piece of the data on hand, lock it away unused, and take it out only when it is time to choose; the error computed on it is called . The handout calls this legal cheating. The analogy in the lecture is working through a study guide: do not do every problem, keep some aside, and use them to test yourself before the exam.
Fun Time: one class for each sign of the first weight
Question (page 7): For , consider two hypothesis sets, and . The first hypothesis set contains all perceptrons with , and the second hypothesis set contains all perceptrons with . Denote and as the minimum- hypothesis in each hypothesis set, respectively. Which statement below is true?
- If , then is the minimum- hypothesis of all perceptrons in .
- If , then is the minimum- hypothesis of all perceptrons in .
- The two hypothesis sets are disjoint.
- None of the above.
Answer: 1. The of every perceptron is either at least 0 or at most 0, so is all perceptrons. Apply the formula from a moment ago: the smaller of the two sets' minimum is the minimum over the union. Since has the smaller one, it has the smallest of all perceptrons. This question is really the second problem from page 4 asked again in a different form.
Choice 2 is wrong because and were picked by . Comparing the of those two only tells you which of the two is better, and other perceptrons may have a lower . For choice 3, the handout's answer explains it: perceptrons with belong to both sides, so the two sets are not disjoint.
Holding out K examples as a validation set: what the guarantee is, why you retrain after selecting, and how large K should be
(Video 00:00–04:01 is how the validation set is split off and its guarantee, 04:01–07:36 the model selection procedure and retraining, 07:36–09:38 the experiment figure, 09:38–11:59 the dilemma about , followed by the rule of thumb and Fun Time.)
The split and the notation
Page 8 splits into two pieces:
| The original way | With validation | |
|---|---|---|
| Data given to the algorithm | ( examples) | ( examples) |
| Resulting hypothesis | ||
| Error used to select the model | , computed on | , computed on |
is called the validation set, and the handout describes it as an on-hand simulation of the test set. The minus sign on , as the lecture explains, is a reminder that it used less data: with 1000 examples and 100 held out for validation, the algorithm sees only 900, and the hypothesis it learns may differ from the one learned from 1000.
Two rules govern the split, and each guarantees one thing.
- Select at random. was drawn independently and identically distributed (iid) from in the first place, so when examples are selected from it uniformly at random, is still iid from the same . This connects with .
- The algorithm sees only . This makes sure is clean.
The lecture describes this arrangement as a division of roles: originally played two roles by itself, being used both to train and to compute for making choices, and the data got contaminated as a result. Now the two roles go to two pieces of data, one responsible for training and one for reporting how well does.
Where the guarantee comes from
The handout writes the conclusion directly on page 8:
The lecture only says that this is the same situation as with a moment ago, where you cut off a small piece yourself to serve as test data, and that it uses the finite-hypothesis Hoeffding inequality. The derivation below is an addition in this article; it uses only results from Foundations Lecture 4 and applies to bounded errors such as 0/1 error.
Step 1: a single hypothesis. First hold fixed. is determined by alone, so as far as is concerned it is a hypothesis fixed in advance. is its average error on iid examples, and the expected error on each of these examples is . This is exactly the setting of Hoeffding, only with the number of examples changed from to :
Step 2: hypotheses together. What we want is a statement that holds no matter which one ends up selected, so we require that none of the hypotheses goes wrong. The probability that some one of them goes wrong is no more than the sum of their individual probabilities of going wrong:
Step 3: solve for . Set the right-hand side equal to , take logarithms, and rearrange:
So with probability at least , every satisfies , and the selected of course satisfies it too. Absorbing the constants into gives the handout's formula. The guarantee for is the same derivation with replaced by .
Plug in some numbers, taking and :
| Validation set size | |
|---|---|
| 200 | 0.144 |
| 20 | 0.455 |
| 1 | 2.04 |
At the error bar is about 14 percentage points; at it comes out above 1, which for 0/1 error guarantees nothing at all. matters much less: at , changing from 100 to the 1296 that the table of combinations multiplied out to only moves the number from 0.144 to 0.165, because sits inside a logarithm.
The key to this derivation is Step 1: must not depend on . Once the algorithm has peeked at the validation set, is no longer a hypothesis fixed in advance and Hoeffding does not apply. That is what "clean" means mathematically.
The model selection procedure, and the final retraining
Page 9 replaces the error in the selection formula with :
The right side of the same page has a flow chart, which says the following:
- Split into and .
- Feed to each of to get .
- Use to compute the validation errors of each .
- Pick the best, which gives .
- Feed the full to , retrain, and output .
Step 5 is the one that is easy to miss. After model number 37 is selected, what you return is retrained on all the data, not the used while selecting. The reason is written as an inequality:
Next to it the handout writes learning curve, remember?: the more data, the lower usually is, which is the learning curve seen in Foundations Lecture 9 and Foundations Lecture 13.
This inequality does not have the same standing as the one derived in "Where the guarantee comes from". The handout calls it heuristic gain and prints it in gray. The lecture makes a point of saying that this part is not a theoretical guarantee and is closer to practical experience; it might be provable with a pile of extra conditions, but the point of this lecture is to use up as much of the data on hand as possible. Joining the two gives the formula at the bottom of page 9:
The inequality on the left rests on experience; the one on the right rests on Hoeffding.
The experiment in the handout: four curves
Page 10 uses validation to select between the degree-five polynomials and the degree-ten polynomials . The horizontal axis of the figure is the validation set size (ticks at 5, 15, 25, with the curves starting to the left of 5), and the vertical axis is the expected (ticks at 0.48, 0.52, 0.56), where lower is better. There are four curves:
| Curve | What it does | How it looks in the figure |
|---|---|---|
| in-sample (solid black horizontal line) | Selection with | Does not change with ; height about 0.545 |
| optimal (dashed black line; the matching caption on the right is printed in gray) | Cheating-selection with | The lowest, about 0.47, below the 0.48 tick |
| sub-g (red) | Selection with , reporting | Below the black line at the far left (about 0.535), then rising all the way; crosses the black line at around 8 to 9 and has left the top of the figure at 0.56 by around 11 |
| full-g (blue) | Selection with , reporting | Starts at about the same height as the red curve and then stays below both the black and the red; lowest at about (about 0.52), rising slightly to about 0.53 at |
All the values in the "How it looks in the figure" column are approximate values read off the figure by eye for this article; the handout prints no numbers. The lecture goes through the curves one by one. Selecting with picks the degree-ten polynomial every time, because its is always smaller, so it is a horizontal line. The optimal line is drawn dashed with a gray caption to show that it cannot be done in practice, only by cheating. The blue curve is always below the red one, confirming that is better than ; the blue curve is also below the black line, showing that selecting with validation really is safer than selecting with .
The bottom of the slide leaves a question: why is sub-g worse than in-sample some time? The handout gives no answer, and the lecture does: as grows, shrinks, and is learned from less data and performs worse. Even if you choose the right model among these , what you return can still lose to a that used all the data, even though the latter was selected by .
The dilemma about K
Page 11 writes the reasoning of validation as a chain of approximations:
We choose using the rightmost quantity, , while what we want to end up with is the one that is best on the leftmost, . The two approximations pull in opposite directions:
- Large : every is close to (the right approximation is good), but all are much worse than (the left approximation is bad). This is the right half of the figure, where the red and blue curves are far apart.
- Small : every is close to (left is good), but is far from (right is bad). The row of the table in "Where the guarantee comes from" is this case.
The practical rule of thumb the handout gives is
The lecture adds that some people use one tenth or one twentieth, and one fifth is a very common number.
Six data points: one validation split
Back to the six data points made up for this article. is not an integer, so take here. Suppose the random draw puts points 2 and 5 in the validation set:
The constant model. is the average of the four values in the training set: . On the validation set, and , so the average is .
The line model. Least squares in one dimension is the slope formula. The training set has and :
, so . At it predicts , with squared error ; at it predicts , with squared error . .
The quadratic model. The solution is . It predicts at and at , the squared errors are and , and .
| Model | Error on | |
|---|---|---|
| Constant | 0.6719 | 1.1406 |
| Line | 0.1418 | 0.2960 |
| Quadratic | 0.0012 | 0.4054 |
The quadratic model passes almost exactly through the four training points (error 0.0012), yet on the two points it has not seen it loses to the line. The line has the smallest , so the line model is the one selected. Following step 5 of the procedure, retrain on all six points; what is returned is the computed earlier.
What this result means. This data was made up, so the answer can be checked. Suppose is uniformly distributed on and the noise has mean 0. Then equals plus the variance of the noise; the latter is the same for every hypothesis, so only the former needs comparing:
| Returned hypothesis | Mean squared distance from |
|---|---|
| Constant, all six points | 0.5208 |
| Line, all six points () | 0.0017 |
| Quadratic, all six points | 0.0495 |
| Line, only four points () | 0.0349 |
Of the three models the line is closest to , so chose correctly, and the quadratic that chose is worse by a factor of nearly 30. The last two rows compare retraining: for the same line model, the from six points is much closer than the from four, so holds in this example.
What about a different split. Holding out two of six points can be done in ways, and this article computed all of them: 7 select the line, 2 are ties between the line and the quadratic (the trained quadratic coefficient happens to be 0), 5 select the quadratic, and 1 selects the constant. The one that selects the constant holds out the last two points (points 5 and 6): the training set is left with the four points from to , where climbs all the way, so both the line and the quadratic overshoot toward the upper right, the three values are 1.2656, 1.4062, and 5.0781, and the constant comes out smallest.
A validation error with is just the average of two numbers, and a different split can change the conclusion. That is the starting point of the next section.
Fun Time: how long does validation take
Question (page 12): For a learning model that takes seconds of training when using examples, what is the total amount of seconds needed when running the whole validation procedure with on 25 such models with different parameters to get the final ?
Answer: 2. The procedure contains two kinds of training. First, each of the 25 models is trained once on , which has examples, so each run takes seconds and 25 runs take seconds. Then the selected model is retrained once on all examples, which takes seconds. The total is .
is the time for training each of the 25 models once on all the data, which is the cost of selecting by ; is that plus one more training run. is too small, since the first stage alone takes .
The lecture draws one more point out of this question: is less than . Validation is not necessarily slower, because less data goes in when training .
Pushing K to 1: leave-one-out cross validation
(Video 00:00–03:53 is and the definition of , 03:53–07:27 illustrates it with three points and uses it to select a model, 07:27–12:57 is the theoretical guarantee, 12:57–15:24 the experiment on handwritten digits, and Fun Time follows.)
One validation example is not enough, so take turns
has to be both large and small, and it is hard to satisfy both sides. Page 13 first looks at an extreme: . Now uses examples and is almost the same as , so the left part of the approximation chain is fine. The trouble is on the right: there is only one validation example.
Suppose the one held out is example . In notation, the validation set is , the hypothesis trained on the other examples is , and the validation error has a single term:
A single tells you very little. The example in the lecture is binary classification: is either 0 or 1, and you cannot tell from it whether the true is 32% or 27%. The handout's remedy is to hold out each example once in turn, obtaining , and then average:
This is called leave-one-out cross validation. The lecture takes the name apart: leave-one-out means leaving one example out as validation data; cross means that the same example is sometimes training data and sometimes validation data.
Note that the argument of is , not one particular hypothesis. The values come from different , and the lecture says that what it reports is not how good a single is but how well this algorithm does on this data. What we hope for is .
The three-point figure: line or constant
Page 14 draws two rows of figures with three points. The top row is the linear model: each time one point is removed, the remaining two determine a line, and is the squared vertical distance from the removed point to that line; the three small figures mark , , and . The bottom row is the constant model: the best constant for two points is the average of their values (the lecture calls it the midpoint of the two points), and is again the squared distance from the removed point to that horizontal line.
In the first figure of the top row, the two remaining points are close together and at different heights, so the line through them is steep and the removed point is far from it. The lecture points out that the error is squared, so this term is very large and the top row's average is higher than the bottom row's. Leave-one-out is telling you here that with only three points the constant is enough, and a line is already too complex for this data.
The handout's figure has no numbers, so this article makes up three points of a similar shape to compute it once: , , .
| Removed point | Line: through the two remaining points | for the line | Constant: average of the two remaining points | for the constant |
|---|---|---|---|---|
| , which is at | ||||
| , which is at | ||||
| , which is at | ||||
| Average |
and , so the constant is selected. The line loses because of the 25 in the first row.
Using it to select a model means replacing in "The model selection procedure, and the final retraining" with (bottom of page 14):
After selecting, train once on all the data as before to get .
Why it is called almost unbiased
Page 15 asks: does say something about ? The answer is yes, for the average on size- data.
First, be clear about what is to be proved. The lecture puts it this way: take an algorithm, do leave-one-out on 1000 examples, then do it on all sorts of different sets of 1000 examples and average. That average equals the average of the obtained by feeding 999 examples to the same algorithm. The latter is written , which is the height of the learning curve at . The expectation is taken over all data sets of size .
The handout lists only the formulas. The lecture gives the reason for every step, organized below.
Line 1: The leftmost equals sign just substitutes the definition of . The aligned equals sign uses the fact that expectation is linear and summation is linear, so the two can be exchanged.
Line 2: Split into two parts, the held-out example and the other examples, written . The data is drawn iid, so these two parts are independent, and the expectation over can be written as an expectation over first and then over . The definition of is substituted at the same time.
Line 3: Look at the inner layer. depends only on , so inside that layer it is fixed. The average error of a fixed hypothesis on fresh data drawn from is the definition of . The lecture says it makes no difference whether this example is called or . An addition in this article: this step and Step 1 of "Where the guarantee comes from" rest on the same thing, namely that the hypothesis has not seen this example.
The first equals sign of line 4: is iid examples. Averaging over all such data sets is the definition of . This number does not depend on .
The last equals sign of line 4: The average of identical constants is the constant itself.
The conclusion at the bottom of the slide is that the expected says something about the expected , and it is often called almost unbiased estimate of . It is "almost" because the right-hand side is the average performance with examples while what we want is the one with ; the lecture says one uses 999 and the other 1000, which is nearly the same.
This article adds two things to keep in mind when reading this formula. First, it is a statement about the expectation, about what happens after averaging over many data sets; with only one data set on hand, the computed that one time can be far from . Second, the values are not independent of one another, because any two share training examples, so you cannot treat as the average over independent test examples and apply Hoeffding directly.
Checking it once with the average predictor
The following is a check made up for this article, not from the slides. Take the simplest algorithm: ignore and always predict with the average of the training values, with squared error. Suppose is drawn iid with mean and variance .
First compute . Training on examples gives ; a fresh is independent of it, and both have expectation . When independent random variables are subtracted, their variances add:
This agrees with the learning curve for linear regression in Foundations Lecture 9, , at .
Then compute the expectation of . , where is the average of the other examples and is independent of . The same calculation gives:
Every has this expectation, and so does their average. The theorem holds, and you can see where the "almost" falls short:
| 3 | |||
| 1000 |
The column uses the other formula from Foundations Lecture 9, . At the expectation of is somewhat higher than the we actually want, which errs on the pessimistic side; , by contrast, is only half of it. Once is large the gap disappears. This article also ran a simulation in Python: with and , drawing 200,000 data sets, the averages of , , , and are 0.666, 1.499, 1.500, and 1.334, matching the table above.
Leave-one-out on the six data points
Back to the six data points. Each model has to be trained six times, holding out one point each time. Take holding out point 1, , as the example:
- Constant: the average of the other five values is , so .
- Line: the least-squares solution on the other five points is , which predicts at , so .
- Quadratic: the solution on the other five points is , which predicts at , so .
To fit the five points on the right, the quadratic drops to when extended to the left, far from the true . After all six rounds:
| Held-out point | for the constant | for the line | for the quadratic |
|---|---|---|---|
| Point 1 | 2.2500 | 0.0225 | 2.5600 |
| Point 2 | 2.2500 | 0.5933 | 0.7861 |
| Point 3 | 0.0900 | 0.3517 | 0.1012 |
| Point 4 | 0.8100 | 0.3943 | 0.1322 |
| Point 5 | 0.8100 | 0.0037 | 0.0017 |
| Point 6 | 0.8100 | 0.8100 | 0.1600 |
| 1.1700 | 0.3626 | 0.6235 |
selects the line. Compared with a single validation split, there is no question here of which two points happened to be drawn, because every point serves as validation data once. The quadratic model is in fact more accurate than the line on the middle four points; it loses at the two ends, especially at point 1.
The handwritten digit experiment
Page 16 returns to the handwritten digit data used in earlier lectures, telling "1" from "not 1", with two raw features, average intensity and symmetry. These two features go through a polynomial transform, and the thing to decide is how many transformed features to use.
The figure at the lower left has the number of features used on the horizontal axis (ticks at 5, 10, 15, 20, with the curves starting to the left of 5) and the error on the vertical axis (ticks at 0.01, 0.02, 0.03), and it has three curves. The figure prints no values, and the numbers below are all approximate values read off the figure by eye for this article:
- (blue): lower with more features, stepping down like a staircase from about 0.008 to nearly 0.
- (red): lower when few features are used (about 0.014 to 0.017), then jumping to about 0.027 past roughly ten features.
- (black, here the leave-one-out result): lowest in the stretch with few features (touching about 0.008 near 5 and near 7), then rising along with the red curve, and jumping up and down.
The lecture explains that this is real data, so the curves are not as smooth as the earlier averaged figures, but you can see that a good number of features lies in the middle and toward the low side, and more is not better; the black curve is closer to the red one. Selecting by picks the largest number of features, and selecting by , the lecture says, picks around five or seven.
The two figures at the upper right are the classification boundaries obtained by the two ways of selecting: the boundary from select by twists and turns, and the boundary from select by is much smoother. The handout's conclusion is much better than .
Fun Time: the three numbers 1, 5, 7
Question (page 17): Consider three examples , , with , , . A learning algorithm predicts with the average value of the data set, which is the optimal constant prediction with respect to the squared error. What is (squared error) of the algorithm?
Answer: 4. Each time, hold out one example and predict it with the average of the other two:
- Hold out : the other two average , so .
- Hold out : the other two average , so .
- Hold out : the other two average , so .
. The lecture says this question is the calculation behind the three figures in the bottom row of page 14.
The of choice 2 is : the average of the three is , and . You get it if you forget to remove the example being tested from the average. is more than twice , in the same direction as the row of the table in "Checking it once with the average predictor". The 0 of choice 1 is impossible: the three values differ, so a constant has some error no matter what it guesses. The handout does not explain where choice 3 comes from. This article found one calculation that matches: use the median of the three values, 5, as the constant prediction and compute the squared error directly on the three examples, which gives . That uses neither the average nor a held-out example, so it is not the the question asks for.
An addition in this article: the question gives no because the constant model does not need it. If you supply and let a line model compete, the three are , , , and , far below the constant's 14. These three points are nearly on a straight line, and this time leave-one-out selects the line. It has no built-in preference for simple models; it goes by the data.
A version that is affordable and stable enough: V-fold cross validation
(Video 00:00–02:28 is the two disadvantages of leave-one-out, 02:28–05:25 is V-fold, 05:25–08:28 the closing reminders about validation, 08:28–08:59 Fun Time, followed by the summary of the whole lecture and the preview of the next one.)
The two disadvantages of leave-one-out
Page 18 lists two.
Computation. Every model needs additional training runs. The numbers in the lecture are 1000 examples meaning 1000 training runs with 999 examples going in each time, which is often not feasible in practice. The handout mentions an exception: a special case like the analytic solution for linear regression. The lecture says the leave-one-out error of linear regression also has a closed-form solution and can be computed quickly, but most models have no such shortcut.
Stability. The reason the handout gives is variance of single-point estimates, and it shows the figure from page 16 again. The lecture explains that each is measured at a single point. For binary classification it is either 0 or 1, which jumps around a lot, and averaging these jumps away is not easy, so the black curve in the figure jumps. That is bad for selection: we do not want to select a model merely because its curve happened to jump down a notch there.
The handout's conclusion is : not often used practically.
Partitioning into V parts
Page 19 first looks at leave-one-out from another angle: it partitions into parts, taking for training and 1 for validation in turn. The computation is heavy because the partition is too fine.
So partition more coarsely. V-fold cross validation randomly partitions into equal parts, taking 1 for validation and the other for training in turn. The handout's figure has : is partitioned into through , and the figure shows the round in which is the validate part and the other nine are the train parts. The hypothesis from round is written , the error is computed on part , and the rounds are averaged:
The selection formula just swaps the error once more:
The lecture says leave-one-out is one extreme case of cross validation (). With 1000 examples partitioned into 10 parts, the effort is 10 training runs instead of 1000.
The handout's rule of thumb is
The lecture adds that partitioning into 5, 20, or 2 parts is your choice, and 10 is the common one.
This article puts it back into the dilemma about . In V-fold each validation set has examples and each training set has . At every uses nine tenths of the data and is not far from ; the validation error of each round is an average over examples, steadier than a single ; and once all the rounds are done, every example has served as validation data once. It buys all this with times as many training runs.
3-fold on the six data points, and a summary table of the four ways of selecting
Partition the six data points into 3 parts of 2 points each. Suppose the random partition is , , (the numbers are the indices of the points). The round in which the second part is the validation set is the one already computed in "Six data points: one validation split".
| Part used for validation | Constant | Line | Quadratic |
|---|---|---|---|
| Points 1 and 4 | 1.1406 | 0.1956 | 1.2800 |
| Points 2 and 5 | 1.1406 | 0.2960 | 0.4054 |
| Points 3 and 6 | 0.6250 | 0.4450 | 0.4450 |
| 0.9688 | 0.3122 | 0.7101 |
In the third row the line and the quadratic have the same number, because the quadratic coefficient trained in that round happens to be 0. selects the line. Putting the four ways of selecting together:
| Selection by | Constant | Line | Quadratic | Selects | Training runs (including the final retraining) |
|---|---|---|---|---|---|
| 0.8125 | 0.1643 | 0.0929 | Quadratic | 3 | |
| , holding out points 2 and 5 | 1.1406 | 0.2960 | 0.4054 | Line | 3 + 1 |
| , holding out points 5 and 6 | 1.2656 | 1.4062 | 5.0781 | Constant | 3 + 1 |
| 1.1700 | 0.3626 | 0.6235 | Line | 18 + 1 | |
| , 3-fold | 0.9688 | 0.3122 | 0.7101 | Line | 9 + 1 |
| Mean squared distance from after retraining | 0.5208 | 0.0017 | 0.0495 |
This table lines up with several of the handout's conclusions. selects the most complex model. A single validation split may choose correctly, or may choose wrongly because of the two points that were drawn. Leave-one-out and 3-fold both choose correctly, and 3-fold takes only half the training runs. This is just one example with six data points and proves nothing, but you can compute every cell yourself.
The pure Python below prints, for the three models, , the holding out points 2 and 5, , and the 3-fold (the first number in each row is the degree of the polynomial):
from fractions import Fraction as F
def fit(pts, deg): # least squares: build the normal equation, then solve by Gaussian elimination
n = deg + 1
A = [[sum(F(x) ** (i + j) for x, _ in pts) for j in range(n)]
+ [sum(y * F(x) ** i for x, y in pts)] for i in range(n)]
for i in range(n):
p = next(r for r in range(i, n) if A[r][i] != 0)
A[i], A[p] = A[p], A[i]
A[i] = [v / A[i][i] for v in A[i]]
for r in range(n):
if r != i:
A[r] = [a - A[r][i] * c for a, c in zip(A[r], A[i])]
return [row[n] for row in A]
def err(w, pts): # mean squared error
return sum((sum(c * F(x) ** i for i, c in enumerate(w)) - y) ** 2
for x, y in pts) / len(pts)
def cv(pts, deg, folds): # each item of folds holds the indices of one validation part (0-based)
total = 0
for fold in folds:
train = [p for i, p in enumerate(pts) if i not in fold]
total += err(fit(train, deg), [pts[i] for i in fold])
return total / len(folds)
D = list(zip(range(6), [F(1), F(1), F(5, 2), F(3), F(3), F(3)]))
for deg in range(3):
print(deg,
round(float(err(fit(D, deg), D)), 4), # E_in
round(float(cv(D, deg, [[1, 4]])), 4), # hold out points 2 and 5
round(float(cv(D, deg, [[i] for i in range(6)])), 4), # leave-one-out
round(float(cv(D, deg, [[0, 3], [1, 4], [2, 5]])), 4)) # 3-fold
A final reminder: validation is also selecting
Page 20 is titled Final Words on Validation and has two blocks.
'Selecting' the validation tool:
- V-fold is generally preferred over a single validation if computation allows. The reason given in the lecture is that the averaging makes it more stable, at the price of the extra computation.
- 5-fold or 10-fold generally works well, and it is not necessary to trade V-fold with leave-one-out and pay the extra computation.
Nature of validation:
| Stage | What the handout says | The analogy in the lecture |
|---|---|---|
| Training | select among hypotheses | The preliminary round: each class picks its own top student |
| Validation | select among finalists | The semifinal: set another exam and see which of these contestants does well |
| Testing | just evaluate | Once the choice is made this only measures; nothing is being selected |
Because validation is also making a choice, data contamination and the price of model complexity show up there too, only more lightly than in training. The handout's wording is validation still more optimistic than testing: you choose the one that looks best in front of you, and when it is sent out to be tested it may no longer be the best.
So the words at the bottom of the slide are do not fool yourself and others, report test result, not best validation result. The lecture says that some papers pick the parameter with the best validation result among all of them and report that number as the result, and such a number is not that accurate; however well validation is done, it is only the performance that the data on hand tells you.
Why validation is still optimistic: an example you can compute
The following is an example made up for this article, not from the slides. Suppose there are classifiers, all of which guess at random, so the true of each is . Use validation examples to pick the one with the smallest , then report that smallest as its performance. What gets reported on average?
Start with and . Each classifier's on two examples can be , , or , with probabilities , , and . The two classifiers are independent, and we take the smaller one:
- The minimum is : both must be , with probability .
- The minimum is at least : neither can be , with probability ; so the probability that the minimum is exactly is .
- The minimum is : the remaining .
The expectation is . For two coin-flipping classifiers, the average error reported after picking is 0.31, while the true error is 0.5.
Computing the same thing exactly in Python up to and , the expectation of the smallest is 0.284. The more candidates and the less validation data, the larger this gap, in the same direction as the in the guarantee. Only with , when no selection is made at all, is the expectation of exactly 0.5, and that is testing.
Fun Time: how long does 10-fold take
Question (page 21): With the same setting as page 12: for a learning model that takes seconds of training when using examples, what is the total amount of seconds needed when running 10-fold cross validation on 25 such models with different parameters to get the final ?
Answer: 3. The training set in each round is nine parts, examples, taking seconds. Each model needs 10 rounds, so 25 models need 250 rounds:
After is selected, retrain once on all the data and add , for a total of . Choice 4 is twice the answer; choices 1 and 2 are both far below the that the 250 training rounds alone require.
An addition in this article, putting the three approaches together (25 models, training time ):
| Approach | Total time | At |
|---|---|---|
| Single split, | seconds | |
| 10-fold | seconds | |
| leave-one-out | about seconds |
10-fold is about 12 times the single split, and leave-one-out is more than 120 times 10-fold again, a factor that grows with . This is why page 20 says there is no need to switch to leave-one-out.
The four lines of this lecture
The summary on page 22 reduces each of the four sections to one line:
- Model Selection Problem: dangerous by and dishonest by .
- Validation: select with while returning .
- Leave-One-Out Cross Validation: huge computation for almost unbiased estimate.
- V-Fold Cross Validation: reasonable computation and performance.
The preview of the next lecture is a single line: something 'up my sleeve'. In the lecture, Lin says that the core tools have now all been covered, and what comes next is more like a few small secrets: what else to watch out for when using machine learning, followed by a summary of the whole course.
Self-check
1. Four examples have values . The algorithm always predicts with the average of the training values, with squared error. Compute , , and the 2-fold with the data partitioned into and .
Answer: the overall average is , so . Leave-one-out: holding out , the rest average and ; holding out , the rest average and ; holding out , the rest average and ; holding out , the rest average and . . 2-fold: training on gives , and on the error is ; training on gives , and on the error is ; . is the smallest because the examples being tested are themselves in the training data; the difference between the other two comes from the training data dropping from 3 examples to 2, which makes worse.
2. You have candidate models (0/1 error) and a validation set of examples. By the derivation in this article, with 95% confidence, by how much at most can the of the selected model exceed ? If you want this gap to be no more than 0.05, how large must be at least?
Answer: . To bring it down to 0.05 you need .
3. A colleague's procedure is: train for each of 25 parameter settings on all of , then draw one fifth of at random to compute the error of each , and pick the smallest. How does this differ from validation?
Criteria: the answer should point out that the one fifth drawn has already been used for training by every and is not clean; the error computed on it is part of , and is not a hypothesis fixed in advance with respect to this data, so the Hoeffding guarantee does not hold. The result favors the most complex model just as selecting by does. The correct order is to split off first and train only on .
4. After selecting a model, why retrain on all the data? Does this step have a theoretical guarantee? Someone returns right after selecting; in what situation is the loss largest?
Criteria: the answer should mention the learning curve (the more data, the lower usually is); it should say that the handout labels this heuristic and that the lecture states outright it is practical experience and not a theoretical guarantee; it should say that the loss is larger the larger is and the smaller is, and connect this to the red curve (sub-g) on page 10, which at large is even higher than the black line that selects by .
5. Three classifiers guess at random (the true of each is ). Using only validation example, pick the one with the smallest and report that smallest . What is the expectation of the reported value? What does this show?
Answer: each classifier's is 0 or 1, each with probability . The minimum is 1 only when all three guess wrong, with probability , so the expectation is , far below the true . The validation error after picking is an optimistic estimate, and what should be reported is the test result.
Exercises and corresponding reading
How the two semesters map. The Fall 2024 course page puts Foundations Lectures 15 and 16 in the same week (W8, 10/21), and the slides for this lecture are 15u_handout.pdf. The Fall 2026 course page also puts it in W8 (10/28); when checked on 2026-10-13 the slide link for that week still returned 404, so this article uses only the MOOC version of the handout. The four videos for this lecture are videos 58 through 61 of the Foundations YouTube playlist.
Textbook. Both course pages map this lecture to Section 4.3 of Learning from Data (LFD).
Homework. The related problems are in Fall 2024 HW5 (released 2024-11-04, due 11-18, 12 problems plus 1 bonus; Q1–4 are auto-graded and Q5–12 are graded by the TAs), which has no official solutions.
| Problem | Content | Where it maps in this lecture |
|---|---|---|
| Q2 | On linearly separable data, for the decision stump whose threshold is the midpoint of the closest positive and negative points, find the tightest upper bound on the leave-one-out error | leave-one-out |
| Q7 | Compute the average on the first examples and validate on the last ; find the expected validation error | validation set and |
| Q8 | Prove that for constant regression , showing that is more optimistic than | leave-one-out |
| Q11 | The 2-versus-6 subproblem of mnist.scale: split off 8000 examples as the sub-training set, select with , then retrain on all the training data; repeat 1126 times and compare the distribution of with Q10 (which selects with ) | validation plus retraining |
| Q12 | The same, but selecting with 3-fold cross validation, compared with Q11 | V-fold |
Q8 has no official solution, so you can check it with numbers first: the of the Fun Time on page 17 have and , and ; the of Self-check question 1 matches as well. For Q7, the calculation in "Checking it once with the average predictor" is a starting point.
The three problems Q10 through Q12 use the public LIBLINEAR and mnist.scale, and they share the same set of candidates () and the same rule of breaking ties by the largest , so it is best to write them as one program that only swaps the selection criterion and see directly how much the distributions of the three ways of selecting differ. The L1 regularization in Q10 is in the previous lecture. A guide to the whole assignment is in the Foundations homework guide.
Hands-on. While watching the second video, write out the five steps of the validation procedure yourself and circle the step "retrain on the full ". After the last two videos, use a tool you know (scikit-learn, for example) to run 5-fold and leave-one-out once each on the same small data set, and compare the time taken and how much the estimate moves when you switch to a different random partition.
Further reading. Another treatment of the same topic: Stanford CS229 Lecture 6: Bias and Variance, Regularization, and Model Selection.
Next lecture. Foundations Lecture 16: Three Learning Principles is the last lecture of Foundations. It teaches no new algorithm and is about the situations in which the guarantees so far break down; this lecture's "contamination" gets a formal name there.
Course video sources
Original videos: Model Selection Problem, Validation, Leave-One-Out Cross Validation, V-Fold Cross Validation
Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.
Update Log
- 2026-10-13: Previously combined with Lecture 16 in one article; changed to one article per lecture and rewritten from the handout and the video transcripts.
- 2026-10-10: Marked the video status and checked the recording sources and how they were obtained.
- 2026-10-10: Rechecked the video status. The embedded videos match the lectures in the official playlist.
References
- Foundations Lecture 15 handout, 15_handout.pdf: the main source for this article. The content of the four sections, the four Fun Time questions, and the figures on pages 3, 9, 10, 14, 16, and 19 all come from this handout.
- Foundations Lecture 4 handout, 04_handout.pdf: the finite-hypothesis Hoeffding inequality, which this article uses to fill in the derivation of the validation guarantee.
- Foundations Lecture 9 handout, 09_handout.pdf: the two formulas for the linear regression learning curve, used to compare against the check with the average predictor.
- Machine Learning Foundations YouTube playlist: the IDs and titles of the four videos; what the text reports as said "in the lecture" is paraphrased from machine transcripts of these four videos.
- Hsuan-Tien Lin > MOOCs: the official entry for handouts and recordings.
- Machine Learning, Fall 2024 course page: the corresponding week, the 15u slides, and the LFD section.
- Machine Learning, Fall 2026 course page: the corresponding week and LFD section.
- Fall 2024 Homework 5 (hw5.pdf): the content of Q2, Q7, Q8, Q11, and Q12.
- LIBLINEAR and LIBSVM datasets: mnist.scale: the tool and data used by the programming problems of HW5.
- Learning from Data textbook website: the source of Section 4.3.
- Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques: Course Overview: where this lecture sits in the whole course and the reading routes.