Generalization
A model generalizes when its error on new data from deployment stays close to its training error. Suppose the pairs come from an unknown joint distribution . Training measures the empirical risk on the training pairs. The goal is the population risk on fresh draws from :
Here is the model and is the loss, such as squared error or cross-entropy. Empirical risk minimization (ERM) picks from a model class . The training error is . The generalization gap is usually positive, since was chosen to make small. The error on an independent test set estimates .A fitted model can fail in two ways:
- Underfitting: the model class is too simple for the signal, so training and test error are both high and close together.
- Overfitting: the model fits the noise in the training sample, so training error is low and test error is much higher.
Data play three roles, and each role needs its own data:
- Training set: fits the parameters , such as the weights .
- Validation set: chooses hyperparameters and models, such as the penalty , the number of hidden units or the stopping epoch.
- Test set: estimates the final error once, after every choice is made.
Common splits are 60/20/20 and 80/10/10. Cross-validation reuses the training data for validation when is small.
Data leakage is any path by which information from the evaluation data reaches training or model selection. With leakage, the held-out error is too optimistic. Four forms are common:
- Shared entities: a random row split puts records of the same patient, user or document on both sides.
- Fitting before splitting: scaling statistics, feature selection or data augmentation use all the data.
- Features from the future: an input is recorded after the moment of prediction, so deployment will not have it.
- Test reuse: the test set guides repeated changes to the model.
To prevent shared entities, split the data at the deployment unit. This is the unit that will be new when the model is used. It may be a patient, hospital, user, time period or near-duplicate cluster. A held-out estimate is valid only if the evaluation data represent deployment. Distribution shift is a failure of that assumption. Deep networks can fit random labels perfectly (Zhang et al., 2017). A low training error alone therefore says nothing about generalization.
Intuition
To generalize means to go from particular cases to a general rule. A model sees examples and must work on every future input from the same source. Each training label mixes signal, which repeats in new data, with noise, which does not. A flexible model can fit both. A model that fits the noise gets a lower training error. The model was chosen and scored on the same data. On held-out data the fit to the noise does not help, since new labels carry independent noise. Overfitting means fitting the training sample more closely than its signal supports. Underfitting is the opposite case, where the model cannot represent the signal.
A held-out estimate is trustworthy under two conditions. No information from the held-out data may reach any choice about the model. The held-out data must also look like the data the model will meet in deployment. A leaky pipeline violates the first condition. A change in the data between training and deployment violates the second.
The animation fits polynomials of degree 1 to 11 to the same 12 training points. The right panel tracks training and test error. Test error is lowest at degree 3.
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\newcommand{\tp}[1]{\filldraw[draw=black, fill=cold] (#1) circle (2.6pt);}
\newcommand{\ts}[1]{\filldraw[draw=mid, thick, fill=white] (#1) circle (2.2pt);}
\newcommand{\tr}[1]{\filldraw[draw=black, fill=cold] (#1) circle (1.9pt);}
\newcommand{\te}[1]{\filldraw[draw=hot, thick, fill=white] (#1) circle (2.9pt);}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw (0.00,-2.2) rectangle (6.40,2.2);
\draw[mid] (0.00,0) -- (6.40,0);
\draw (0.20,-2.2) -- (0.20,-2.10) node[pos=0, below] {\footnotesize $0$};
\draw (3.20,-2.2) -- (3.20,-2.10) node[pos=0, below] {\footnotesize $0.5$};
\draw (6.20,-2.2) -- (6.20,-2.10) node[pos=0, below] {\footnotesize $1$};
\draw[mid, dashed, thick] (0.20,0.00) -- (0.33,0.13) -- (0.45,0.26) -- (0.57,0.38) -- (0.70,0.50) -- (0.82,0.61) -- (0.95,0.71) -- (1.07,0.79) -- (1.20,0.87) -- (1.32,0.92) -- (1.45,0.97) -- (1.57,0.99) -- (1.70,1.00) -- (1.82,0.99) -- (1.95,0.97) -- (2.08,0.92) -- (2.20,0.87) -- (2.33,0.79) -- (2.45,0.71) -- (2.58,0.61) -- (2.70,0.50) -- (2.83,0.38) -- (2.95,0.26) -- (3.08,0.13) -- (3.20,0.00) -- (3.32,-0.13) -- (3.45,-0.26) -- (3.58,-0.38) -- (3.70,-0.50) -- (3.83,-0.61) -- (3.95,-0.71) -- (4.07,-0.79) -- (4.20,-0.87) -- (4.33,-0.92) -- (4.45,-0.97) -- (4.58,-0.99) -- (4.70,-1.00) -- (4.83,-0.99) -- (4.95,-0.97) -- (5.08,-0.92) -- (5.20,-0.87) -- (5.33,-0.79) -- (5.45,-0.71) -- (5.58,-0.61) -- (5.70,-0.50) -- (5.83,-0.38) -- (5.95,-0.26) -- (6.08,-0.13) -- (6.20,-0.00);
\ts{0.76,0.56}
\ts{0.82,0.32}
\ts{0.82,0.59}
\ts{1.05,0.60}
\ts{1.38,0.93}
\ts{1.82,0.83}
\ts{1.88,0.78}
\ts{1.96,0.99}
\ts{2.38,0.67}
\ts{2.60,0.60}
\ts{2.76,0.34}
\ts{2.90,0.46}
\ts{2.94,0.07}
\ts{3.07,0.33}
\ts{3.78,-0.24}
\ts{3.90,-0.73}
\ts{3.93,-0.26}
\ts{4.01,-0.24}
\ts{4.06,-0.68}
\ts{4.25,-1.23}
\ts{4.30,-1.32}
\ts{4.35,-0.98}
\ts{4.41,-0.51}
\ts{4.42,-0.81}
\ts{4.65,-0.81}
\ts{4.97,-1.02}
\ts{5.31,-0.99}
\ts{5.86,-0.26}
\ts{5.95,-0.53}
\ts{6.09,-0.17}
\tp{0.59,0.25}
\tp{0.80,0.75}
\tp{1.51,1.09}
\tp{1.94,0.53}
\tp{2.51,0.81}
\tp{2.79,0.27}
\tp{3.29,-0.16}
\tp{3.84,-0.77}
\tp{4.62,-1.10}
\tp{4.88,-0.96}
\tp{5.48,-0.66}
\tp{6.12,-0.47}
\draw[->] (8.3,-2.2) -- (14.88,-2.2);
\node at (11.66,-2.95) {\footnotesize polynomial degree};
\draw[->] (8.3,-2.2) -- (8.3,2.50);
\node at (11.38,2.95) {MSE (log scale)};
\draw (8.22,-2.20) -- (8.38,-2.20) node[pos=0, left] {\footnotesize $0.001$};
\draw (8.22,-1.32) -- (8.38,-1.32) node[pos=0, left] {\footnotesize $0.01$};
\draw (8.22,-0.44) -- (8.38,-0.44) node[pos=0, left] {\footnotesize $0.1$};
\draw (8.22,0.44) -- (8.38,0.44) node[pos=0, left] {\footnotesize $1$};
\draw (8.22,1.32) -- (8.38,1.32) node[pos=0, left] {\footnotesize $10$};
\draw (8.22,2.20) -- (8.38,2.20) node[pos=0, left] {\footnotesize $100$};
\draw (8.86,-2.28) -- (8.86,-2.12) node[pos=0, below] {\footnotesize $1$};
\draw (9.98,-2.28) -- (9.98,-2.12) node[pos=0, below] {\footnotesize $3$};
\draw (11.10,-2.28) -- (11.10,-2.12) node[pos=0, below] {\footnotesize $5$};
\draw (12.22,-2.28) -- (12.22,-2.12) node[pos=0, below] {\footnotesize $7$};
\draw (13.34,-2.28) -- (13.34,-2.12) node[pos=0, below] {\footnotesize $9$};
\draw (14.46,-2.28) -- (14.46,-2.12) node[pos=0, below] {\footnotesize $11$};
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.6s = "true", 19.1s = "true", repeats}}}]
\te{8.86,-0.19}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.6s = "true", 19.1s = "true", repeats}}}]
\tr{8.86,-0.21}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (8.86,-0.21) -- (9.42,-0.22);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (8.86,-0.19) -- (9.42,-0.20);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 19.1s = "true", repeats}}}]
\te{9.42,-0.20}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 19.1s = "true", repeats}}}]
\tr{9.42,-0.22}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.3s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (9.42,-0.22) -- (9.98,-0.84);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.3s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (9.42,-0.20) -- (9.98,-0.69);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.3s = "true", 19.1s = "true", repeats}}}]
\te{9.98,-0.69}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.3s = "true", 19.1s = "true", repeats}}}]
\tr{9.98,-0.84}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.5s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (9.98,-0.84) -- (10.54,-0.93);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.5s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (9.98,-0.69) -- (10.54,-0.62);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.5s = "true", 19.1s = "true", repeats}}}]
\te{10.54,-0.62}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.5s = "true", 19.1s = "true", repeats}}}]
\tr{10.54,-0.93}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.1s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (10.54,-0.93) -- (11.10,-0.98);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.1s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (10.54,-0.62) -- (11.10,-0.61);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.1s = "true", 19.1s = "true", repeats}}}]
\te{11.10,-0.61}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.1s = "true", 19.1s = "true", repeats}}}]
\tr{11.10,-0.98}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (11.10,-0.98) -- (11.66,-1.10);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (11.10,-0.61) -- (11.66,-0.57);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 19.1s = "true", repeats}}}]
\te{11.66,-0.57}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 19.1s = "true", repeats}}}]
\tr{11.66,-1.10}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.3s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (11.66,-1.10) -- (12.22,-1.20);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.3s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (11.66,-0.57) -- (12.22,-0.48);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.3s = "true", 19.1s = "true", repeats}}}]
\te{12.22,-0.48}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.3s = "true", 19.1s = "true", repeats}}}]
\tr{12.22,-1.20}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.9s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (12.22,-1.20) -- (12.78,-1.22);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.9s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (12.22,-0.48) -- (12.78,-0.39);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.9s = "true", 19.1s = "true", repeats}}}]
\te{12.78,-0.39}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.9s = "true", 19.1s = "true", repeats}}}]
\tr{12.78,-1.22}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.5s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (12.78,-1.22) -- (13.34,-1.31);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.5s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (12.78,-0.39) -- (13.34,-0.38);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.5s = "true", 19.1s = "true", repeats}}}]
\te{13.34,-0.38}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.5s = "true", 19.1s = "true", repeats}}}]
\tr{13.34,-1.31}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (13.34,-1.31) -- (13.90,-1.55);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (13.34,-0.38) -- (13.90,0.21);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 19.1s = "true", repeats}}}]
\te{13.90,0.21}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 19.1s = "true", repeats}}}]
\tr{13.90,-1.55}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.9s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick] (13.90,-1.55) -- (14.46,-2.20);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.9s = "true", 19.1s = "true", repeats}}}]
\draw[hot, thick] (13.90,0.21) -- (14.46,1.45);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.9s = "true", 19.1s = "true", repeats}}}]
\te{14.46,1.45}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.9s = "true", 19.1s = "true", repeats}}}]
\draw[cold, thick, ->] (14.35,-2.07) -- (14.46,-2.20);
\node[anchor=west] at (14.54,-1.92) {\footnotesize $0$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.6s = "false", 19.1s = "false", repeats}}}]
\node[anchor=west] at (-0.1,-3.45) {Fit polynomials of growing degree to the 12 training points. The test points stay hidden.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.6s = "true", 4.6s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,0.96) -- (0.30,0.93) -- (0.40,0.90) -- (0.50,0.86) -- (0.60,0.83) -- (0.70,0.80) -- (0.80,0.76) -- (0.90,0.73) -- (1.00,0.70) -- (1.10,0.66) -- (1.20,0.63) -- (1.30,0.60) -- (1.40,0.56) -- (1.50,0.53) -- (1.60,0.50) -- (1.70,0.46) -- (1.80,0.43) -- (1.90,0.40) -- (2.00,0.36) -- (2.10,0.33) -- (2.20,0.30) -- (2.30,0.26) -- (2.40,0.23) -- (2.50,0.20) -- (2.60,0.16) -- (2.70,0.13) -- (2.80,0.10) -- (2.90,0.06) -- (3.00,0.03) -- (3.10,-0.00) -- (3.20,-0.04) -- (3.30,-0.07) -- (3.40,-0.10) -- (3.50,-0.14) -- (3.60,-0.17) -- (3.70,-0.20) -- (3.80,-0.24) -- (3.90,-0.27) -- (4.00,-0.30) -- (4.10,-0.34) -- (4.20,-0.37) -- (4.30,-0.40) -- (4.40,-0.44) -- (4.50,-0.47) -- (4.60,-0.50) -- (4.70,-0.54) -- (4.80,-0.57) -- (4.90,-0.60) -- (5.00,-0.64) -- (5.10,-0.67) -- (5.20,-0.70) -- (5.30,-0.74) -- (5.40,-0.77) -- (5.50,-0.80) -- (5.60,-0.84) -- (5.70,-0.87) -- (5.80,-0.90) -- (5.90,-0.94) -- (6.00,-0.97) -- (6.10,-1.00) -- (6.20,-1.04);
\node at (3.20,2.95) {degree $1$};
\node at (3.20,2.5) {\footnotesize training MSE $0.18$, \ test MSE $0.19$};
\draw[mid] (8.86,-2.2) -- (8.86,2.20);
\node[anchor=west] at (-0.1,-3.45) {A degree-1 fit is a line. It is too stiff, and both errors are high.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 5.3s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,1.03) -- (0.30,0.99) -- (0.40,0.95) -- (0.50,0.91) -- (0.60,0.87) -- (0.70,0.84) -- (0.80,0.80) -- (0.90,0.76) -- (1.00,0.72) -- (1.10,0.68) -- (1.20,0.64) -- (1.30,0.61) -- (1.40,0.57) -- (1.50,0.53) -- (1.60,0.49) -- (1.70,0.46) -- (1.80,0.42) -- (1.90,0.38) -- (2.00,0.35) -- (2.10,0.31) -- (2.20,0.28) -- (2.30,0.24) -- (2.40,0.21) -- (2.50,0.17) -- (2.60,0.14) -- (2.70,0.10) -- (2.80,0.07) -- (2.90,0.03) -- (3.00,-0.00) -- (3.10,-0.04) -- (3.20,-0.07) -- (3.30,-0.10) -- (3.40,-0.14) -- (3.50,-0.17) -- (3.60,-0.20) -- (3.70,-0.23) -- (3.80,-0.27) -- (3.90,-0.30) -- (4.00,-0.33) -- (4.10,-0.36) -- (4.20,-0.39) -- (4.30,-0.42) -- (4.40,-0.46) -- (4.50,-0.49) -- (4.60,-0.52) -- (4.70,-0.55) -- (4.80,-0.58) -- (4.90,-0.61) -- (5.00,-0.64) -- (5.10,-0.67) -- (5.20,-0.70) -- (5.30,-0.73) -- (5.40,-0.75) -- (5.50,-0.78) -- (5.60,-0.81) -- (5.70,-0.84) -- (5.80,-0.87) -- (5.90,-0.90) -- (6.00,-0.92) -- (6.10,-0.95) -- (6.20,-0.98);
\node at (3.20,2.95) {degree $2$};
\node at (3.20,2.5) {\footnotesize training MSE $0.18$, \ test MSE $0.19$};
\draw[mid] (9.42,-2.2) -- (9.42,2.20);
\node[anchor=west] at (-0.1,-3.45) {Degree 2 is still too stiff.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.3s = "true", 7.5s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,-0.16) -- (0.30,0.01) -- (0.40,0.16) -- (0.50,0.30) -- (0.60,0.42) -- (0.70,0.52) -- (0.80,0.62) -- (0.90,0.69) -- (1.00,0.76) -- (1.10,0.81) -- (1.20,0.85) -- (1.30,0.88) -- (1.40,0.90) -- (1.50,0.90) -- (1.60,0.90) -- (1.70,0.89) -- (1.80,0.87) -- (1.90,0.84) -- (2.00,0.80) -- (2.10,0.76) -- (2.20,0.70) -- (2.30,0.65) -- (2.40,0.59) -- (2.50,0.52) -- (2.60,0.45) -- (2.70,0.37) -- (2.80,0.30) -- (2.90,0.22) -- (3.00,0.13) -- (3.10,0.05) -- (3.20,-0.04) -- (3.30,-0.12) -- (3.40,-0.20) -- (3.50,-0.29) -- (3.60,-0.37) -- (3.70,-0.45) -- (3.80,-0.53) -- (3.90,-0.60) -- (4.00,-0.68) -- (4.10,-0.74) -- (4.20,-0.81) -- (4.30,-0.86) -- (4.40,-0.91) -- (4.50,-0.96) -- (4.60,-1.00) -- (4.70,-1.03) -- (4.80,-1.05) -- (4.90,-1.06) -- (5.00,-1.07) -- (5.10,-1.06) -- (5.20,-1.05) -- (5.30,-1.02) -- (5.40,-0.98) -- (5.50,-0.93) -- (5.60,-0.87) -- (5.70,-0.79) -- (5.80,-0.70) -- (5.90,-0.60) -- (6.00,-0.48) -- (6.10,-0.34) -- (6.20,-0.19);
\node at (3.20,2.95) {degree $3$};
\node at (3.20,2.5) {\footnotesize training MSE $0.035$, \ test MSE $0.051$};
\draw[mid] (9.98,-2.2) -- (9.98,2.20);
\node[anchor=west] at (-0.1,-3.45) {Degree 3 follows the curve. Both errors drop sharply.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.5s = "true", 8.1s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,-0.63) -- (0.30,-0.35) -- (0.40,-0.09) -- (0.50,0.13) -- (0.60,0.33) -- (0.70,0.50) -- (0.80,0.64) -- (0.90,0.76) -- (1.00,0.85) -- (1.10,0.93) -- (1.20,0.98) -- (1.30,1.02) -- (1.40,1.04) -- (1.50,1.04) -- (1.60,1.03) -- (1.70,1.01) -- (1.80,0.97) -- (1.90,0.93) -- (2.00,0.87) -- (2.10,0.81) -- (2.20,0.74) -- (2.30,0.67) -- (2.40,0.59) -- (2.50,0.50) -- (2.60,0.41) -- (2.70,0.32) -- (2.80,0.23) -- (2.90,0.14) -- (3.00,0.04) -- (3.10,-0.05) -- (3.20,-0.14) -- (3.30,-0.23) -- (3.40,-0.31) -- (3.50,-0.40) -- (3.60,-0.48) -- (3.70,-0.55) -- (3.80,-0.62) -- (3.90,-0.68) -- (4.00,-0.74) -- (4.10,-0.80) -- (4.20,-0.84) -- (4.30,-0.88) -- (4.40,-0.92) -- (4.50,-0.94) -- (4.60,-0.96) -- (4.70,-0.98) -- (4.80,-0.98) -- (4.90,-0.98) -- (5.00,-0.97) -- (5.10,-0.95) -- (5.20,-0.93) -- (5.30,-0.90) -- (5.40,-0.86) -- (5.50,-0.82) -- (5.60,-0.77) -- (5.70,-0.71) -- (5.80,-0.65) -- (5.90,-0.58) -- (6.00,-0.51) -- (6.10,-0.43) -- (6.20,-0.34);
\node at (3.20,2.95) {degree $4$};
\node at (3.20,2.5) {\footnotesize training MSE $0.028$, \ test MSE $0.063$};
\draw[mid] (10.54,-2.2) -- (10.54,2.20);
\node[anchor=west] at (-0.1,-3.45) {Training error keeps falling. Test error starts to rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.1s = "true", 8.7s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,-0.06) -- (0.30,0.05) -- (0.40,0.16) -- (0.50,0.27) -- (0.60,0.38) -- (0.70,0.48) -- (0.80,0.58) -- (0.90,0.67) -- (1.00,0.75) -- (1.10,0.81) -- (1.20,0.87) -- (1.30,0.92) -- (1.40,0.95) -- (1.50,0.97) -- (1.60,0.98) -- (1.70,0.98) -- (1.80,0.96) -- (1.90,0.93) -- (2.00,0.90) -- (2.10,0.85) -- (2.20,0.79) -- (2.30,0.72) -- (2.40,0.65) -- (2.50,0.56) -- (2.60,0.48) -- (2.70,0.38) -- (2.80,0.28) -- (2.90,0.18) -- (3.00,0.08) -- (3.10,-0.03) -- (3.20,-0.13) -- (3.30,-0.23) -- (3.40,-0.33) -- (3.50,-0.43) -- (3.60,-0.52) -- (3.70,-0.61) -- (3.80,-0.69) -- (3.90,-0.76) -- (4.00,-0.82) -- (4.10,-0.88) -- (4.20,-0.93) -- (4.30,-0.97) -- (4.40,-0.99) -- (4.50,-1.01) -- (4.60,-1.02) -- (4.70,-1.02) -- (4.80,-1.00) -- (4.90,-0.98) -- (5.00,-0.95) -- (5.10,-0.91) -- (5.20,-0.87) -- (5.30,-0.82) -- (5.40,-0.77) -- (5.50,-0.71) -- (5.60,-0.65) -- (5.70,-0.60) -- (5.80,-0.55) -- (5.90,-0.51) -- (6.00,-0.47) -- (6.10,-0.46) -- (6.20,-0.45);
\node at (3.20,2.95) {degree $5$};
\node at (3.20,2.5) {\footnotesize training MSE $0.025$, \ test MSE $0.064$};
\draw[mid] (11.10,-2.2) -- (11.10,2.20);
\node[anchor=west] at (-0.1,-3.45) {Training error keeps falling. Test error starts to rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 9.3s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,-1.53) -- (0.30,-0.91) -- (0.40,-0.40) -- (0.50,-0.01) -- (0.60,0.30) -- (0.70,0.54) -- (0.80,0.71) -- (0.90,0.84) -- (1.00,0.93) -- (1.10,0.98) -- (1.20,1.01) -- (1.30,1.02) -- (1.40,1.01) -- (1.50,0.99) -- (1.60,0.96) -- (1.70,0.93) -- (1.80,0.89) -- (1.90,0.85) -- (2.00,0.80) -- (2.10,0.75) -- (2.20,0.70) -- (2.30,0.64) -- (2.40,0.58) -- (2.50,0.52) -- (2.60,0.45) -- (2.70,0.38) -- (2.80,0.31) -- (2.90,0.22) -- (3.00,0.14) -- (3.10,0.05) -- (3.20,-0.04) -- (3.30,-0.14) -- (3.40,-0.24) -- (3.50,-0.34) -- (3.60,-0.44) -- (3.70,-0.54) -- (3.80,-0.64) -- (3.90,-0.73) -- (4.00,-0.82) -- (4.10,-0.90) -- (4.20,-0.96) -- (4.30,-1.02) -- (4.40,-1.07) -- (4.50,-1.10) -- (4.60,-1.11) -- (4.70,-1.11) -- (4.80,-1.09) -- (4.90,-1.06) -- (5.00,-1.00) -- (5.10,-0.94) -- (5.20,-0.86) -- (5.30,-0.77) -- (5.40,-0.67) -- (5.50,-0.58) -- (5.60,-0.49) -- (5.70,-0.41) -- (5.80,-0.36) -- (5.90,-0.34) -- (6.00,-0.37) -- (6.10,-0.46) -- (6.20,-0.63);
\node at (3.20,2.95) {degree $6$};
\node at (3.20,2.5) {\footnotesize training MSE $0.018$, \ test MSE $0.070$};
\draw[mid] (11.66,-2.2) -- (11.66,2.20);
\node[anchor=west] at (-0.1,-3.45) {Training error keeps falling. Test error starts to rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.3s = "true", 9.9s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.28,-2.20) -- (0.30,-1.96) -- (0.40,-0.97) -- (0.50,-0.25) -- (0.60,0.25) -- (0.70,0.60) -- (0.80,0.82) -- (0.90,0.95) -- (1.00,1.02) -- (1.10,1.04) -- (1.20,1.03) -- (1.30,1.00) -- (1.40,0.96) -- (1.50,0.93) -- (1.60,0.89) -- (1.70,0.86) -- (1.80,0.83) -- (1.90,0.80) -- (2.00,0.77) -- (2.10,0.74) -- (2.20,0.71) -- (2.30,0.67) -- (2.40,0.63) -- (2.50,0.58) -- (2.60,0.52) -- (2.70,0.45) -- (2.80,0.37) -- (2.90,0.28) -- (3.00,0.18) -- (3.10,0.07) -- (3.20,-0.04) -- (3.30,-0.16) -- (3.40,-0.28) -- (3.50,-0.40) -- (3.60,-0.52) -- (3.70,-0.63) -- (3.80,-0.74) -- (3.90,-0.83) -- (4.00,-0.91) -- (4.10,-0.98) -- (4.20,-1.04) -- (4.30,-1.07) -- (4.40,-1.09) -- (4.50,-1.10) -- (4.60,-1.09) -- (4.70,-1.06) -- (4.80,-1.03) -- (4.90,-0.98) -- (5.00,-0.92) -- (5.10,-0.86) -- (5.20,-0.80) -- (5.30,-0.74) -- (5.40,-0.69) -- (5.50,-0.64) -- (5.60,-0.60) -- (5.70,-0.57) -- (5.80,-0.55) -- (5.90,-0.53) -- (6.00,-0.51) -- (6.10,-0.48) -- (6.20,-0.43);
\node at (3.20,2.95) {degree $7$};
\node at (3.20,2.5) {\footnotesize training MSE $0.014$, \ test MSE $0.091$};
\draw[mid] (12.22,-2.2) -- (12.22,2.20);
\node[anchor=west] at (-0.1,-3.45) {Training error keeps falling. Test error starts to rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.9s = "true", 10.5s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,-1.81) -- (0.30,-1.17) -- (0.40,-0.61) -- (0.50,-0.13) -- (0.60,0.26) -- (0.70,0.57) -- (0.80,0.79) -- (0.90,0.94) -- (1.00,1.04) -- (1.10,1.08) -- (1.20,1.09) -- (1.30,1.07) -- (1.40,1.03) -- (1.50,0.98) -- (1.60,0.92) -- (1.70,0.87) -- (1.80,0.81) -- (1.90,0.77) -- (2.00,0.73) -- (2.10,0.69) -- (2.20,0.65) -- (2.30,0.62) -- (2.40,0.59) -- (2.50,0.55) -- (2.60,0.50) -- (2.70,0.45) -- (2.80,0.39) -- (2.90,0.31) -- (3.00,0.22) -- (3.10,0.12) -- (3.20,0.01) -- (3.30,-0.11) -- (3.40,-0.24) -- (3.50,-0.38) -- (3.60,-0.51) -- (3.70,-0.64) -- (3.80,-0.77) -- (3.90,-0.88) -- (4.00,-0.98) -- (4.10,-1.06) -- (4.20,-1.12) -- (4.30,-1.15) -- (4.40,-1.16) -- (4.50,-1.15) -- (4.60,-1.12) -- (4.70,-1.07) -- (4.80,-1.00) -- (4.90,-0.93) -- (5.00,-0.85) -- (5.10,-0.78) -- (5.20,-0.72) -- (5.30,-0.68) -- (5.40,-0.66) -- (5.50,-0.67) -- (5.60,-0.69) -- (5.70,-0.73) -- (5.80,-0.77) -- (5.90,-0.77) -- (6.00,-0.71) -- (6.10,-0.53) -- (6.20,-0.15);
\node at (3.20,2.95) {degree $8$};
\node at (3.20,2.5) {\footnotesize training MSE $0.013$, \ test MSE $0.11$};
\draw[mid] (12.78,-2.2) -- (12.78,2.20);
\node[anchor=west] at (-0.1,-3.45) {Training error keeps falling. Test error starts to rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.5s = "true", 11.7s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.24,2.20) -- (0.28,1.65) -- (0.31,1.14) -- (0.35,0.76) -- (0.39,0.49) -- (0.43,0.32) -- (0.46,0.21) -- (0.50,0.17) -- (0.54,0.18) -- (0.57,0.22) -- (0.61,0.28) -- (0.65,0.37) -- (0.69,0.46) -- (0.73,0.57) -- (0.76,0.67) -- (0.80,0.77) -- (0.84,0.86) -- (0.88,0.95) -- (0.91,1.03) -- (0.95,1.10) -- (0.99,1.16) -- (1.03,1.20) -- (1.06,1.24) -- (1.10,1.26) -- (1.14,1.27) -- (1.18,1.28) -- (1.21,1.27) -- (1.25,1.26) -- (1.29,1.24) -- (1.32,1.21) -- (1.36,1.18) -- (1.40,1.15) -- (1.44,1.11) -- (1.48,1.07) -- (1.51,1.03) -- (1.55,0.99) -- (1.59,0.95) -- (1.62,0.91) -- (1.66,0.87) -- (1.70,0.83) -- (1.74,0.80) -- (1.78,0.77) -- (1.81,0.74) -- (1.85,0.71) -- (1.89,0.69) -- (1.93,0.67) -- (1.96,0.65) -- (2.00,0.64) -- (2.04,0.63) -- (2.08,0.62) -- (2.11,0.61) -- (2.15,0.60) -- (2.19,0.60) -- (2.23,0.60) -- (2.26,0.59) -- (2.30,0.59) -- (2.34,0.59) -- (2.38,0.59) -- (2.41,0.58) -- (2.45,0.58) -- (2.49,0.57) -- (2.53,0.57) -- (2.56,0.56) -- (2.60,0.55) -- (2.64,0.53) -- (2.68,0.51) -- (2.71,0.49) -- (2.75,0.47) -- (2.79,0.45) -- (2.83,0.42) -- (2.86,0.39) -- (2.90,0.35) -- (2.94,0.32) -- (2.98,0.28) -- (3.01,0.23) -- (3.05,0.19) -- (3.09,0.14) -- (3.13,0.09) -- (3.16,0.04) -- (3.20,-0.01) -- (3.24,-0.06) -- (3.28,-0.12) -- (3.31,-0.17) -- (3.35,-0.23) -- (3.39,-0.28) -- (3.42,-0.33) -- (3.46,-0.39) -- (3.50,-0.44) -- (3.54,-0.49) -- (3.58,-0.54) -- (3.61,-0.59) -- (3.65,-0.64) -- (3.69,-0.68) -- (3.73,-0.72) -- (3.76,-0.76) -- (3.80,-0.80) -- (3.84,-0.83) -- (3.88,-0.86) -- (3.91,-0.89) -- (3.95,-0.91) -- (3.99,-0.94) -- (4.03,-0.96) -- (4.06,-0.97) -- (4.10,-0.99) -- (4.14,-1.00) -- (4.18,-1.01) -- (4.21,-1.01) -- (4.25,-1.02) -- (4.29,-1.02) -- (4.33,-1.03) -- (4.36,-1.03) -- (4.40,-1.03) -- (4.44,-1.03) -- (4.48,-1.03) -- (4.51,-1.03) -- (4.55,-1.03) -- (4.59,-1.02) -- (4.63,-1.02) -- (4.66,-1.02) -- (4.70,-1.02) -- (4.74,-1.02) -- (4.78,-1.02) -- (4.81,-1.02) -- (4.85,-1.02) -- (4.89,-1.02) -- (4.93,-1.02) -- (4.96,-1.02) -- (5.00,-1.02) -- (5.04,-1.01) -- (5.08,-1.01) -- (5.11,-1.00) -- (5.15,-0.99) -- (5.19,-0.97) -- (5.23,-0.95) -- (5.26,-0.92) -- (5.30,-0.89) -- (5.34,-0.86) -- (5.38,-0.81) -- (5.41,-0.76) -- (5.45,-0.71) -- (5.49,-0.64) -- (5.53,-0.57) -- (5.56,-0.49) -- (5.60,-0.41) -- (5.64,-0.32) -- (5.68,-0.23) -- (5.71,-0.15) -- (5.75,-0.06) -- (5.79,0.02) -- (5.83,0.10) -- (5.86,0.15) -- (5.90,0.19) -- (5.94,0.20) -- (5.98,0.17) -- (6.01,0.10) -- (6.05,-0.04) -- (6.09,-0.24) -- (6.13,-0.52) -- (6.16,-0.91) -- (6.20,-1.42);
\node at (3.20,2.95) {degree $9$};
\node at (3.20,2.5) {\footnotesize training MSE $0.010$, \ test MSE $0.12$};
\draw[mid] (13.34,-2.2) -- (13.34,2.20);
\node[anchor=west] at (-0.1,-3.45) {The fit bends toward single noisy points. Test error grows.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 12.9s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.45,2.20) -- (0.46,1.82) -- (0.50,1.07) -- (0.54,0.59) -- (0.57,0.32) -- (0.61,0.20) -- (0.65,0.19) -- (0.69,0.27) -- (0.73,0.41) -- (0.76,0.58) -- (0.80,0.76) -- (0.84,0.94) -- (0.88,1.12) -- (0.91,1.28) -- (0.95,1.41) -- (0.99,1.53) -- (1.03,1.61) -- (1.06,1.67) -- (1.10,1.70) -- (1.14,1.71) -- (1.18,1.70) -- (1.21,1.67) -- (1.25,1.62) -- (1.29,1.56) -- (1.32,1.49) -- (1.36,1.41) -- (1.40,1.33) -- (1.44,1.24) -- (1.48,1.16) -- (1.51,1.07) -- (1.55,0.99) -- (1.59,0.92) -- (1.62,0.85) -- (1.66,0.79) -- (1.70,0.73) -- (1.74,0.69) -- (1.78,0.65) -- (1.81,0.62) -- (1.85,0.60) -- (1.89,0.59) -- (1.93,0.58) -- (1.96,0.57) -- (2.00,0.58) -- (2.04,0.58) -- (2.08,0.59) -- (2.11,0.61) -- (2.15,0.62) -- (2.19,0.63) -- (2.23,0.65) -- (2.26,0.66) -- (2.30,0.67) -- (2.34,0.68) -- (2.38,0.68) -- (2.41,0.68) -- (2.45,0.68) -- (2.49,0.67) -- (2.53,0.66) -- (2.56,0.64) -- (2.60,0.62) -- (2.64,0.59) -- (2.68,0.56) -- (2.71,0.53) -- (2.75,0.49) -- (2.79,0.45) -- (2.83,0.40) -- (2.86,0.35) -- (2.90,0.30) -- (2.94,0.25) -- (2.98,0.20) -- (3.01,0.14) -- (3.05,0.09) -- (3.09,0.03) -- (3.13,-0.02) -- (3.16,-0.08) -- (3.20,-0.13) -- (3.24,-0.18) -- (3.28,-0.23) -- (3.31,-0.28) -- (3.35,-0.32) -- (3.39,-0.36) -- (3.42,-0.41) -- (3.46,-0.44) -- (3.50,-0.48) -- (3.54,-0.51) -- (3.58,-0.55) -- (3.61,-0.58) -- (3.65,-0.60) -- (3.69,-0.63) -- (3.73,-0.66) -- (3.76,-0.68) -- (3.80,-0.70) -- (3.84,-0.73) -- (3.88,-0.75) -- (3.91,-0.77) -- (3.95,-0.80) -- (3.99,-0.82) -- (4.03,-0.84) -- (4.06,-0.87) -- (4.10,-0.89) -- (4.14,-0.92) -- (4.18,-0.94) -- (4.21,-0.97) -- (4.25,-0.99) -- (4.29,-1.02) -- (4.33,-1.04) -- (4.36,-1.06) -- (4.40,-1.08) -- (4.44,-1.10) -- (4.48,-1.11) -- (4.51,-1.12) -- (4.55,-1.13) -- (4.59,-1.13) -- (4.63,-1.13) -- (4.66,-1.12) -- (4.70,-1.10) -- (4.74,-1.08) -- (4.78,-1.06) -- (4.81,-1.02) -- (4.85,-0.98) -- (4.89,-0.94) -- (4.93,-0.89) -- (4.96,-0.84) -- (5.00,-0.78) -- (5.04,-0.73) -- (5.08,-0.67) -- (5.11,-0.61) -- (5.15,-0.56) -- (5.19,-0.52) -- (5.23,-0.48) -- (5.26,-0.45) -- (5.30,-0.44) -- (5.34,-0.44) -- (5.38,-0.47) -- (5.41,-0.51) -- (5.45,-0.58) -- (5.49,-0.68) -- (5.53,-0.80) -- (5.56,-0.95) -- (5.60,-1.13) -- (5.64,-1.33) -- (5.68,-1.55) -- (5.71,-1.79) -- (5.75,-2.03) -- (5.78,-2.20);
\draw[very thick] (6.04,-2.20) -- (6.05,-2.07) -- (6.09,-1.35) -- (6.13,-0.29) -- (6.16,1.18) -- (6.18,2.20);
\node at (3.20,2.95) {degree $10$};
\node at (3.20,2.5) {\footnotesize training MSE $0.0054$, \ test MSE $0.54$};
\draw[mid] (13.90,-2.2) -- (13.90,2.20);
\node[anchor=west] at (-0.1,-3.45) {Training error keeps falling while test error grows fast.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.9s = "true", 15.5s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.55,2.20) -- (0.57,0.92) -- (0.61,-0.38) -- (0.65,-0.89) -- (0.69,-0.87) -- (0.73,-0.49) -- (0.76,0.09) -- (0.80,0.78) -- (0.84,1.47) -- (0.88,2.13) -- (0.88,2.20);
\draw[very thick] (1.36,2.20) -- (1.36,2.19) -- (1.40,1.88) -- (1.44,1.59) -- (1.48,1.32) -- (1.51,1.08) -- (1.55,0.87) -- (1.59,0.70) -- (1.62,0.56) -- (1.66,0.46) -- (1.70,0.39) -- (1.74,0.35) -- (1.78,0.34) -- (1.81,0.36) -- (1.85,0.39) -- (1.89,0.44) -- (1.93,0.50) -- (1.96,0.57) -- (2.00,0.65) -- (2.04,0.72) -- (2.08,0.79) -- (2.11,0.85) -- (2.15,0.90) -- (2.19,0.95) -- (2.23,0.98) -- (2.26,1.00) -- (2.30,1.00) -- (2.34,1.00) -- (2.38,0.98) -- (2.41,0.94) -- (2.45,0.90) -- (2.49,0.85) -- (2.53,0.79) -- (2.56,0.72) -- (2.60,0.65) -- (2.64,0.57) -- (2.68,0.50) -- (2.71,0.42) -- (2.75,0.34) -- (2.79,0.27) -- (2.83,0.21) -- (2.86,0.14) -- (2.90,0.09) -- (2.94,0.04) -- (2.98,-0.00) -- (3.01,-0.04) -- (3.05,-0.07) -- (3.09,-0.09) -- (3.13,-0.11) -- (3.16,-0.13) -- (3.20,-0.14) -- (3.24,-0.15) -- (3.28,-0.16) -- (3.31,-0.16) -- (3.35,-0.17) -- (3.39,-0.19) -- (3.42,-0.20) -- (3.46,-0.22) -- (3.50,-0.25) -- (3.54,-0.28) -- (3.58,-0.32) -- (3.61,-0.36) -- (3.65,-0.42) -- (3.69,-0.48) -- (3.73,-0.54) -- (3.76,-0.61) -- (3.80,-0.69) -- (3.84,-0.77) -- (3.88,-0.85) -- (3.91,-0.94) -- (3.95,-1.02) -- (3.99,-1.10) -- (4.03,-1.17) -- (4.06,-1.24) -- (4.10,-1.31) -- (4.14,-1.36) -- (4.18,-1.40) -- (4.21,-1.44) -- (4.25,-1.46) -- (4.29,-1.46) -- (4.33,-1.46) -- (4.36,-1.44) -- (4.40,-1.41) -- (4.44,-1.37) -- (4.48,-1.33) -- (4.51,-1.27) -- (4.55,-1.21) -- (4.59,-1.15) -- (4.63,-1.09) -- (4.66,-1.04) -- (4.70,-0.99) -- (4.74,-0.95) -- (4.78,-0.93) -- (4.81,-0.92) -- (4.85,-0.94) -- (4.89,-0.97) -- (4.93,-1.03) -- (4.96,-1.11) -- (5.00,-1.20) -- (5.04,-1.32) -- (5.08,-1.44) -- (5.11,-1.58) -- (5.15,-1.71) -- (5.19,-1.83) -- (5.23,-1.92) -- (5.26,-1.98) -- (5.30,-1.98) -- (5.34,-1.91) -- (5.38,-1.76) -- (5.41,-1.49) -- (5.45,-1.10) -- (5.49,-0.56) -- (5.53,0.13) -- (5.56,1.00) -- (5.60,2.04) -- (5.60,2.20);
\draw[very thick] (6.10,2.20) -- (6.13,-1.74) -- (6.13,-2.20);
\node at (3.20,2.95) {degree $11$};
\node at (3.20,2.5) {\footnotesize training MSE $0$, \ test MSE $14$};
\draw[mid] (14.46,-2.2) -- (14.46,2.20);
\node[anchor=west] at (-0.1,-3.45) {Degree 11 has 12 coefficients, so the fit passes through all 12 points. Training error is 0.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.5s = "true", 19.1s = "false", 19.1s = "false", repeats}}}]
\draw[very thick] (0.20,-0.16) -- (0.30,0.01) -- (0.40,0.16) -- (0.50,0.30) -- (0.60,0.42) -- (0.70,0.52) -- (0.80,0.62) -- (0.90,0.69) -- (1.00,0.76) -- (1.10,0.81) -- (1.20,0.85) -- (1.30,0.88) -- (1.40,0.90) -- (1.50,0.90) -- (1.60,0.90) -- (1.70,0.89) -- (1.80,0.87) -- (1.90,0.84) -- (2.00,0.80) -- (2.10,0.76) -- (2.20,0.70) -- (2.30,0.65) -- (2.40,0.59) -- (2.50,0.52) -- (2.60,0.45) -- (2.70,0.37) -- (2.80,0.30) -- (2.90,0.22) -- (3.00,0.13) -- (3.10,0.05) -- (3.20,-0.04) -- (3.30,-0.12) -- (3.40,-0.20) -- (3.50,-0.29) -- (3.60,-0.37) -- (3.70,-0.45) -- (3.80,-0.53) -- (3.90,-0.60) -- (4.00,-0.68) -- (4.10,-0.74) -- (4.20,-0.81) -- (4.30,-0.86) -- (4.40,-0.91) -- (4.50,-0.96) -- (4.60,-1.00) -- (4.70,-1.03) -- (4.80,-1.05) -- (4.90,-1.06) -- (5.00,-1.07) -- (5.10,-1.06) -- (5.20,-1.05) -- (5.30,-1.02) -- (5.40,-0.98) -- (5.50,-0.93) -- (5.60,-0.87) -- (5.70,-0.79) -- (5.80,-0.70) -- (5.90,-0.60) -- (6.00,-0.48) -- (6.10,-0.34) -- (6.20,-0.19);
\node at (3.20,2.95) {degree $3$};
\node at (3.20,2.5) {\footnotesize training MSE $0.035$, \ test MSE $0.051$};
\draw[black, dashed] (9.98,-2.2) -- (9.98,2.20);
\node[anchor=west, fill=white, inner sep=1pt] at (10.10,1.06) {\footnotesize lowest test error};
\node[anchor=west] at (8.44,2.25) {\footnotesize $\leftarrow$ underfitting};
\node[anchor=east] at (14.71,2.25) {\footnotesize overfitting $\rightarrow$};
\node[anchor=west] at (-0.1,-3.45) {Degree 3 has the lowest test error. Lower degrees underfit; higher degrees overfit.};
\end{scope}
\node[anchor=west] at (-0.1,-4.0) {\footnotesize blue: training points and training MSE, \ hollow: test points, \ red: test MSE, \ dashed: true curve $\sin 2\pi x$};
\end{tikzpicture}
\end{document}Properties
(Training Error Is Optimistic) The training error of the fitted model underestimates its population risk on average.
Not Taught in Lecture
The exact least-squares gap follows Hastie, Tibshirani and Friedman, The Elements of Statistical Learning, section 7.4. They call it the optimism of the training error.
Fix before seeing the data. Then , because each term of the average is an independent draw of the loss. The fitted is not fixed. Let minimize over . Then on every training set, and taking expectations gives
The left side is the expected training error. The right side is the expected population risk of the fitted model. For least squares the gap is known exactly. Take coefficients, fixed inputs, noise variance , and a true mean that is linear in the features. The expected training MSE is then . The expected MSE on fresh responses at the same inputs is . With and , these are and . With the fit passes through every point, and the training error is zero. A team that picks the best of several models by validation error gets the same downward bias in that error. The test set therefore stays untouched until the end.
(Augmentation Leakage) Augmenting the data before splitting it puts transformed copies of one example on both sides of the split.
A flipped or cropped copy of an image is nearly the same input with the same label. If augmentation runs first and the split second, most validation images have a near-copy in the training set. The model can then score well on validation by recalling the copy. Validation accuracy stays close to training accuracy, while accuracy on a separately collected test set is much lower. The correct order is to split first and augment only the training part. The same order applies to every step that is fitted on data, such as scaling, imputation and feature selection. Each step is fitted on the training part alone and then applied unchanged to the held-out part.
(Split at the Deployment Unit) The held-out unit must be the unit that is new in deployment, with no other difference from training.
Kapoor and Narayanan (2023) documented leakage in published studies across many scientific fields. Dependence between training and test samples was one of its main types. A speech recognizer meant for new speakers needs a split by speaker. A model that forecasts next month’s sales needs a split by time. All training data then come from before the test period, as in time-series cross-validation. A diagnostic model meant for new hospitals needs a split by hospital. A group-wise split assigns every record of one unit to the same side. The opposite mistake also occurs. A test set chosen to be as different as possible adds a shift that deployment will not have. It then underestimates performance.
(Shortcuts Across Sites) A test set drawn from the training sites cannot detect a shortcut that works at all of those sites.
Zech et al. (2018) trained a CNN to detect pneumonia in chest X-rays. Training and internal test images came from two hospital systems, and a third system was held out entirely. The internal test AUC was , and the AUC at the unseen hospital system was . The CNN could identify the source hospital of an image with over accuracy. It used scanner signatures, patient positioning and text overlays. Pneumonia prevalence was at one site and about at another. A model that saw only the hospital identity reached an AUC of on the pooled internal test set. Part of the CNN’s internal score therefore came from recognizing the hospital. That part was lost at a new site. Only a split by hospital would have shown the drop before deployment. 2.2.2. ROC Curve explains how to read these AUC values.
(Networks Can Memorize Random Labels) A network that fits true labels can also fit random labels, so its model class cannot explain its generalization.
Zhang et al. (2017) replaced the CIFAR-10 labels with random ones. Standard image networks still reached training accuracy, while test accuracy fell to chance, . Training took only a small constant factor longer than with the true labels. As the fraction of random labels grew from to , test error rose steadily. Training error stayed at zero. Explicit regularizers such as weight decay and dropout did not prevent the memorization. Turning off the regularizers and data augmentation lowered test accuracy on the true labels by only a few points. Zhang et al. also gave a construction. A two-layer ReLU network with weights can fit any labeling of points in . Some generalization bounds depend only on the richness of the model class, such as its VC dimension or Rademacher complexity. These bounds give no useful guarantee for such networks. Researchers instead look to the data and the training algorithm for an explanation. One candidate is the preference of gradient descent for small-norm solutions, discussed under double descent in 2.3.1. Bias-Variance Decomposition.
(Kinds of Distribution Shift) The joint distribution can change through the inputs, through the label frequencies or through the relationship between them.
Not Taught in Lecture
The three kinds of shift follow the chapter notes of Prince, Understanding Deep Learning, chapter 8, and Moreno-Torres et al. (2012).
Write . Under covariate shift, changes and stays. A pedestrian detector trained on daytime photos and used at night is an example. Under prior shift, changes and stays, as when a disease becomes more common during an outbreak. Under concept shift, itself changes, as when spammers adapt to a filter. Data can also drift slowly after deployment, so deployed models need monitoring. The pneumonia data show the first two kinds at once, since both the images and the prevalence differed across hospitals.
Examples
(Easy) Reading Training and Validation Error
A food-delivery service predicts delivery time in minutes. It fits three models on one training set and scores them on one validation set:
Model Training RMSE Validation RMSE Linear regression 9.8 10.1 Boosted trees, depth 4 6.2 7.0 One very deep tree 0.4 8.9 Which model underfits, which overfits, and which should the team deploy? minutes, so it underfits. The deep tree has almost zero training error and a gap of minutes, so it overfits. The boosted trees have the lowest validation error, so the team picks them. Their validation error of minutes is slightly optimistic, because the team used it to choose among three models. The team should report the chosen model's error on a test set that played no part in the choice.
The linear model has high error on both sets and a gap of only
(Medium) Repeat Visits Across a Random Split
A clinic has visit records from patients, five visits each. Each record holds lab values and a diagnosis. The team splits the records at random, for training and for validation.
What fraction of validation records have another visit of the same patient in training? Why does it matter, and how should the team split? records include the patient's four other visits, and of those records are in validation. All four other visits land in validation with probability So of validation records share a patient with the training set. A simulation of random splits gives the same fraction. One patient's lab values and diagnoses are similar from visit to visit. The model can score well by recognizing the patient instead of reading the disease from the lab values. A new patient in deployment has no earlier visits in the training data. The team should assign whole patients to one side, for example patients to training and to validation.
Take one validation record. The other
(Hard) Choosing on the Test Set
A team tries 50 configurations of a spam classifier and scores each on the same test set of 500 emails. Suppose every configuration has true accuracy and that their test errors are independent. The team reports the best test accuracy.
What accuracy does the team report on average, and how should it have used the data? . The largest of 50 independent normal draws lies about standard deviations above the mean on average. The reported accuracy is therefore about . A simulation of the binomial draws gives . The chosen model still has true accuracy . Real configurations make correlated errors, which shrinks the inflation without removing it. The team should compare the 50 configurations on a validation set. It should score only the final choice on the test set.
Each test accuracy averages 500 Bernoulli outcomes, so its standard deviation is
See Also
- 2.3.1. Bias-Variance Decomposition, which splits the expected test error into noise, bias and variance.
- 2.3.2. Cross-Validation, for estimating test error when data are scarce.
- 3.2. Regularization and 4.4. Neural Network Regularization, the main tools against overfitting.
- 2.2.2. ROC Curve, for the AUC values in the pneumonia study.
- 1. Learning Paradigms, for the supervised setting that this note assumes.