Run this notebook online: or Colab:

# 4.4. Model Selection, Underfitting and Overfitting¶

As machine learning scientists, our goal is to discover *patterns*. But
how can we be sure that we have truly discovered a *general* pattern and
not simply memorized our data. For example, imagine that we wanted to
hunt for patterns among genetic markers linking patients to their
dementia status, (let the labels be drawn from the set {*dementia*,
*mild cognitive impairment*, *healthy*}). Because each person’s genes
identify them uniquely (ignoring identical siblings), it is possible to
memorize the entire dataset.

We do not want our model to say *“That’s Bob! I remember him! He has
dementia!* The reason why is simple. When we deploy the model in the
future, we will encounter patients that the model has never seen before.
Our predictions will only be useful if our model has truly discovered a
*general* pattern.

To recapitulate more formally, our goal is to discover patterns that
capture regularities in the underlying population from which our
training set was drawn. If we are successful in this endeavor, then we
could successfully assess risk even for individuals that we have never
encountered before. This problem—how to discover patterns that
*generalize*—is the fundamental problem of machine learning.

The danger is that when we train models, we access just a small sample
of data. The largest public image datasets contain roughly one million
images. More often, we must learn from only thousands or tens of
thousands of data points. In a large hospital system, we might access
hundreds of thousands of medical records. When working with finite
samples, we run the risk that we might discover *apparent* associations
that turn out not to hold up when we collect more data.

The phenomena of fitting our training data more closely than we fit the underlying distribution is called overfitting, and the techniques used to combat overfitting are called regularization. In the previous sections, you might have observed this effect while experimenting with the Fashion-MNIST dataset. If you altered the model structure or the hyper-parameters during the experiment, you might have noticed that with enough nodes, layers, and training epochs, the model can eventually reach perfect accuracy on the training set, even as the accuracy on test data deteriorates.

## 4.4.1. Training Error and Generalization Error¶

In order to discuss this phenomenon more formally, we need to
differentiate between *training error* and *generalization error*. The
training error is the error of our model as calculated on the training
dataset, while generalization error is the expectation of our model’s
error were we to apply it to an infinite stream of additional data
points drawn from the same underlying data distribution as our original
sample.

Problematically, *we can never calculate the generalization error
exactly*. That is because the stream of infinite data is an imaginary
object. In practice, we must *estimate* the generalization error by
applying our model to an independent test set constituted of a random
selection of data points that were withheld from our training set.

The following three thought experiments will help illustrate this situation better. Consider a college student trying to prepare for her final exam. A diligent student will strive to practice well and test her abilities using exams from previous years. Nonetheless, doing well on past exams is no guarantee that she will excel when it matters. For instance, the student might try to prepare by rote learning the answers to the exam questions. This requires the student to memorize many things. She might even remember the answers for past exams perfectly. Another student might prepare by trying to understand the reasons for giving certain answers. In most cases, the latter student will do much better.

Likewise, consider a model that simply uses a lookup table to answer
questions. If the set of allowable inputs is discrete and reasonably
small, then perhaps after viewing *many* training examples, this
approach would perform well. Still this model has no ability to do
better than random guessing when faced with examples that it has never
seen before. In reality the input spaces are far too large to memorize
the answers corresponding to every conceivable input. For example,
consider the black and white \(28\times28\) images. If each pixel
can take one among \(256\) grayscale values, then there are
\(256^{784}\) possible images. That means that there are far more
low-res grayscale thumbnail-sized images than there are atoms in the
universe. Even if we could encounter this data, we could never afford to
store the lookup table.

Last, consider the problem of trying to classify the outcomes of coin
tosses (class 0: heads, class 1: tails) based on some contextual
features that might be available. No matter what algorithm we come up
with, the generalization error will always be \(\frac{1}{2}\).
However, for most algorithms, we should expect our training error to be
considerably lower, depending on the luck of the draw, even if we did
not have any features! Consider the dataset {0, 1, 1, 1, 0, 1}. Our
feature-less algorithm would have to fall back on always predicting the
*majority class*, which appears from our limited sample to be *1*. In
this case, the model that always predicts class 1 will incur an error of
\(\frac{1}{3}\), considerably better than our generalization error.
As we increase the amount of data, the probability that the fraction of
heads will deviate significantly from \(\frac{1}{2}\) diminishes,
and our training error would come to match the generalization error.

### 4.4.1.1. Statistical Learning Theory¶

Since generalization is the fundamental problem in machine learning, you might not be surprised to learn that many mathematicians and theorists have dedicated their lives to developing formal theories to describe this phenomenon. In their eponymous theorem, Glivenko and Cantelli derived the rate at which the training error converges to the generalization error. In a series of seminal papers, Vapnik and Chervonenkis extended this theory to more general classes of functions. This work laid the foundations of Statistical Learning Theory.

In the *standard supervised learning setting*, which we have addressed
up until now and will stick with throughout most of this book, we assume
that both the training data and the test data are drawn *independently*
from *identical* distributions (commonly called the i.i.d. assumption).
This means that the process that samples our data has no *memory*. The
\(2^{\mathrm{nd}}\) example drawn and the \(3^{\mathrm{rd}}\)
drawn are no more correlated than the \(2^{\mathrm{nd}}\) and the
\(2\)-millionth sample drawn.

Being a good machine learning scientist requires thinking critically, and already you should be poking holes in this assumption, coming up with common cases where the assumption fails. What if we train a mortality risk predictor on data collected from patients at UCSF, and apply it on patients at Massachusetts General Hospital? These distributions are simply not identical. Moreover, draws might be correlated in time. What if we are classifying the topics of Tweets. The news cycle would create temporal dependencies in the topics being discussed, violating any assumptions of independence.

Sometimes we can get away with minor violations of the i.i.d. assumption and our models will continue to work remarkably well. After all, nearly every real-world application involves at least some minor violation of the i.i.d. assumption, and yet we have useful tools for face recognition, speech recognition, language translation, etc.

Other violations are sure to cause trouble. Imagine, for example, if we try to train a face recognition system by training it exclusively on university students and then want to deploy it as a tool for monitoring geriatrics in a nursing home population. This is unlikely to work well since college students tend to look considerably different from the elderly.

In subsequent chapters and volumes, we will discuss problems arising from violations of the i.i.d. assumption. For now, even taking the i.i.d. assumption for granted, understanding generalization is a formidable problem. Moreover, elucidating the precise theoretical foundations that might explain why deep neural networks generalize as well as they do continues to vex the greatest minds in learning theory.

When we train our models, we attempt to search for a function that fits
the training data as well as possible. If the function is so flexible
that it can catch on to spurious patterns just as easily as to true
associations, then it might perform *too well* without producing a model
that generalizes well to unseen data. This is precisely what we want to
avoid (or at least control). Many of the techniques in deep learning are
heuristics and tricks aimed at guarding against overfitting.

### 4.4.1.2. Model Complexity¶

When we have simple models and abundant data, we expect the
generalization error to resemble the training error. When we work with
more complex models and fewer examples, we expect the training error to
go down but the generalization gap to grow. What precisely constitutes
model complexity is a complex matter. Many factors govern whether a
model will generalize well. For example a model with more parameters
might be considered more complex. A model whose parameters can take a
wider range of values might be more complex. Often with neural networks,
we think of a model that takes more training steps as more complex, and
one subject to *early stopping* as less complex.

It can be difficult to compare the complexity among members of substantially different model classes (say a decision tree versus a neural network). For now, a simple rule of thumb is quite useful: A model that can readily explain arbitrary facts is what statisticians view as complex, whereas one that has only a limited expressive power but still manages to explain the data well is probably closer to the truth. In philosophy, this is closely related to Popper’s criterion of falsifiability of a scientific theory: a theory is good if it fits data and if there are specific tests that can be used to disprove it. This is important since all statistical estimation is post hoc, i.e., we estimate after we observe the facts, hence vulnerable to the associated fallacy. For now, we will put the philosophy aside and stick to more tangible issues.

In this section, to give you some intuition, we’ll focus on a few factors that tend to influence the generalizability of a model class:

The number of tunable parameters. When the number of tunable parameters, sometimes called the

*degrees of freedom*, is large, models tend to be more susceptible to overfitting.The values taken by the parameters. When weights can take a wider range of values, models can be more susceptible to overfitting.

The number of training examples. It’s trivially easy to overfit a dataset containing only one or two examples even if your model is simple. But overfitting a dataset with millions of examples requires an extremely flexible model.

## 4.4.2. Model Selection¶

In machine learning, we usually select our final model after evaluating several candidate models. This process is called model selection. Sometimes the models subject to comparison are fundamentally different in nature (say, decision trees vs linear models). At other times, we are comparing members of the same class of models that have been trained with different hyperparameter settings.

With multilayer perceptrons, for example, we may wish to compare models with different numbers of hidden layers, different numbers of hidden units, and various choices of the activation functions applied to each hidden layer. In order to determine the best among our candidate models, we will typically employ a validation set.

### 4.4.2.1. Validation Dataset¶

In principle we should not touch our test set until after we have chosen all our hyper-parameters. Were we to use the test data in the model selection process, there is a risk that we might overfit the test data. Then we would be in serious trouble. If we overfit our training data, there is always the evaluation on test data to keep us honest. But if we overfit the test data, how would we ever know?

Thus, we should never rely on the test data for model selection. And yet we cannot rely solely on the training data for model selection either because we cannot estimate the generalization error on the very data that we use to train the model.

The common practice to address this problem is to split our data three
ways, incorporating a *validation set* in addition to the training and
test sets.

In practical applications, the picture gets muddier. While ideally we would only touch the test data once, to assess the very best model or to compare a small number of models to each other, real-world test data is seldom discarded after just one use. We can seldom afford a new test set for each round of experiments.

The result is a murky practice where the boundaries between validation and test data are worryingly ambiguous. Unless explicitly stated otherwise, in the experiments in this book we are really working with what should rightly be called training data and validation data, with no true test sets. Therefore, the accuracy reported in each experiment is really the validation accuracy and not a true test set accuracy. The good news is that we do not need too much data in the validation set. The uncertainty in our estimates can be shown to be of the order of \(\mathcal{O}(n^{-\frac{1}{2}})\).

### 4.4.2.2. \(K\)-Fold Cross-Validation¶

When training data is scarce, we might not even be able to afford to
hold out enough data to constitute a proper validation set. One popular
solution to this problem is to employ \(K\)*-fold
cross-validation*. Here, the original training data is split into
\(K\) non-overlapping subsets. Then model training and validation
are executed \(K\) times, each time training on \(K-1\) subsets
and validating on a different subset (the one not used for training in
that round). Finally, the training and validation error rates are
estimated by averaging over the results from the \(K\) experiments.

## 4.4.3. Underfitting or Overfitting?¶

When we compare the training and validation errors, we want to be
mindful of two common situations: First, we want to watch out for cases
when our training error and validation error are both substantial but
there is a little gap between them. If the model is unable to reduce the
training error, that could mean that our model is too simple (i.e.,
insufficiently expressive) to capture the pattern that we are trying to
model. Moreover, since the *generalization gap* between our training and
validation errors is small, we have reason to believe that we could get
away with a more complex model. This phenomenon is known as
underfitting.

On the other hand, as we discussed above, we want to watch out for the cases when our training error is significantly lower than our validation error, indicating severe overfitting. Note that overfitting is not always a bad thing. With deep learning especially, it is well known that the best predictive models often perform far better on training data than on holdout data. Ultimately, we usually care more about the validation error than about the gap between the training and validation errors.

Whether we overfit or underfit can depend both on the complexity of our model and the size of the available training datasets, two topics that we discuss below.

### 4.4.3.1. Model Complexity¶

To illustrate some classical intuition about overfitting and model complexity, we give an example using polynomials. Given training data consisting of a single feature \(x\) and a corresponding real-valued label \(y\), we try to find the polynomial of degree \(d\)

to estimate the labels \(y\). This is just a linear regression problem where our features are given by the powers of \(x\), the model’s weights are given by \(w_i\), and the bias is given by \(w_0\) since \(x^0 = 1\) for all \(x\). Since this is just a linear regression problem, we can use the squared error as our loss function.

A higher-order polynomial function is more complex than a lower order polynomial function, since the higher-order polynomial has more parameters and the model function’s selection range is wider. Fixing the training dataset, higher-order polynomial functions should always achieve lower (at worst, equal) training error relative to lower degree polynomials. In fact, whenever the data points each have a distinct value of \(x\), a polynomial function with degree equal to the number of data points can fit the training set perfectly. We visualize the relationship between polynomial degree and under- vs over-fitting in Fig. 4.4.1.

### 4.4.3.2. Dataset Size¶

The other big consideration to bear in mind is the dataset size. Fixing
our model, the fewer samples we have in the training dataset, the more
likely (and more severely) we are to encounter overfitting. As we
increase the amount of training data, the generalization error typically
decreases. Moreover, in general, more data never hurts. For a fixed task
and data *distribution*, there is typically a relationship between model
complexity and dataset size. Given more data, we might profitably
attempt to fit a more complex model. Absent sufficient data, simpler
models may be difficult to beat. For many tasks, deep learning only
outperforms linear models when many thousands of training examples are
available. In part, the current success of deep learning owes to the
current abundance of massive datasets due to Internet companies, cheap
storage, connected devices, and the broad digitization of the economy.

## 4.4.4. Polynomial Regression¶

We can now explore these concepts interactively by fitting polynomials to data. To get started we will import our usual packages.

```
%load ../utils/djl-imports
%load ../utils/plot-utils
%load ../utils/DataPoints.java
```

```
import ai.djl.metric.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.math3.special.Gamma;
```

### 4.4.4.1. Generating the Dataset¶

First we need data. Given \(x\), we will use the following cubic polynomial to generate the labels on training and test data:

The noise term \(\epsilon\) obeys a normal distribution with a mean of 0 and a standard deviation of 0.1. We will synthesize 100 samples each for the training set and test set.

```
// utility functions for shuffle data
public void swap(NDArray arr, int i, int j) {
float tmp = arr.getFloat(i);
arr.set(new NDIndex(i), arr.getFloat(j));
arr.set(new NDIndex(j), tmp);
}
public NDArray shuffle(NDArray arr) {
int size = (int) arr.size();
Random rnd = RandomUtils.RANDOM;
for (int i = Math.toIntExact(size) - 1; i > 0; --i) {
swap(arr, i, rnd.nextInt(i));
}
return arr;
}
```

```
int maxDegree = 20; // Maximum degree of the polynomial
// Training and test dataset sizes
int nTrain = 100;
int nTest = 100;
NDManager manager = NDManager.newBaseManager();
NDArray trueW = manager.zeros(new Shape(maxDegree)); // Allocate lots of empty space
NDArray tempArr = manager.create(new float[]{5f, 1.2f, -3.4f, 5.6f});
for (int i = 0; i < tempArr.size(); i++) {
trueW.set(new NDIndex(i), tempArr.getFloat(i));
}
NDArray features = manager.randomNormal(new Shape(nTrain + nTest, 1));
features = shuffle(features);
NDArray polyFeatures = features.pow(manager.arange(maxDegree).reshape(1, -1));
for(int i = 0; i < maxDegree; i ++){
polyFeatures.set(new NDIndex(":, " + i), polyFeatures.get(":, " + i).div(Gamma.gamma(i+1)));
}
// NDArray factorialArr = factorial(manager.arange(maxDegree).add(1.0f).toType(DataType.FLOAT32, false)).reshape(1, -1);
// polyFeatures = polyFeatures.div(factorialArr);
// Shape of `labels`: (`n_train` + `n_test`,)
NDArray labels = polyFeatures.dot(trueW);
labels = labels.add(manager.randomNormal(0, 0.1f, labels.getShape(), DataType.FLOAT32));
```

For optimization, we typically want to avoid very large values of
gradients, losses, etc. This is why the monomials stored in
`polyDeatures`

are rescaled from \(x^i\) to
\(\frac{1}{i!} x^i\). It allows us to avoid very large values for
large exponents \(i\).

Take a look at the first 2 samples from the generated dataset. The value 1 is technically a feature, namely the constant feature corresponding to the bias.

```
System.out.println("features: " + features.get(":2"));
System.out.println("polyFeatures: " + polyFeatures.get(":2"));
System.out.println("labels: " + labels.get(":2"));
```

```
features: ND: (2, 1) gpu(0) float32
[[-2.2623],
[ 1.4805],
]
polyFeatures: ND: (2, 20) gpu(0) float32
[[ 1.00000000e+00, -2.26231217e+00, 2.55902815e+00, -1.92977345e+00, 1.09143758e+00, -4.93834496e-01, 1.86201289e-01, -6.01779222e-02, 1.70176551e-02, -4.27769450e-03, 9.67747997e-04, -1.99031652e-04, 3.75226446e-05, -6.52984136e-06, 1.05518131e-06, -1.59143298e-07, 2.25019896e-08, -2.99450154e-09, 3.76360970e-10, -4.48129450e-11],
[ 1.00000000e+00, 1.48046422e+00, 1.09588718e+00, 5.40807247e-01, 2.00161442e-01, 5.92663735e-02, 1.46236233e-02, 3.09282169e-03, 5.72351448e-04, 9.41495382e-05, 1.39385029e-05, 1.87595037e-06, 2.31439799e-07, 2.63567959e-08, 2.78716361e-09, 2.75086398e-10, 2.54534727e-11, 2.21664462e-12, 1.82314613e-13, 1.42058022e-14],
]
labels: ND: (2) gpu(0) float32
[-17.119 , 5.917 ]
```

### 4.4.4.2. Training and Testing Model¶

Let us first implement a function to evaluate the loss on a given data.

```
int logInterval = 20;
int numEpochs = Integer.getInteger("MAX_EPOCH", 400);
public ArrayDataset loadArray(NDArray features, NDArray labels, int batchSize, boolean shuffle) {
return new ArrayDataset.Builder()
.setData(features) // set the features
.optLabels(labels) // set the labels
.setSampling(batchSize, shuffle) // set the batch size and random sampling
.build();
}
double[] trainLoss;
double[] testLoss;
double[] epochCount;
trainLoss = new double[numEpochs/logInterval];
testLoss = new double[numEpochs/logInterval];
epochCount = new double[numEpochs/logInterval];
```

Now define the training function.

```
NDArray weight = null;
public void train(NDArray trainFeatures, NDArray testFeatures, NDArray trainLabels, NDArray testLabels, int nDegree)
throws IOException, TranslateException {
Loss l2Loss = Loss.l2Loss();
NDManager manager = NDManager.newBaseManager();
Tracker lrt = Tracker.fixed(0.01f);
Optimizer sgd = Optimizer.sgd().setLearningRateTracker(lrt).build();
DefaultTrainingConfig config = new DefaultTrainingConfig(l2Loss)
.optDevices(Engine.getInstance().getDevices(1)) // single GPU
.optOptimizer(sgd) // Optimizer (loss function)
.addTrainingListeners(TrainingListener.Defaults.basic()); // Logging
Model model = Model.newInstance("mlp");
SequentialBlock net = new SequentialBlock();
// Switch off the bias since we already catered for it in the polynomial
// features
Linear linearBlock = Linear.builder().optBias(false).setUnits(1).build();
net.add(linearBlock);
model.setBlock(net);
Trainer trainer = model.newTrainer(config);
int batchSize = Math.min(10, (int) trainLabels.getShape().get(0));
ArrayDataset trainIter = loadArray(trainFeatures, trainLabels, batchSize, true);
ArrayDataset testIter = loadArray(testFeatures, testLabels, batchSize, true);
trainer.initialize(new Shape(1, nDegree));
System.out.println("Start Training...");
for (int epoch = 1; epoch <= numEpochs; epoch++) {
// Iterate over dataset
for (Batch batch : trainer.iterateDataset(trainIter)) {
// Update loss and evaulator
EasyTrain.trainBatch(trainer, batch);
// Update parameters
trainer.step();
batch.close();
}
// reset training and validation evaluators at end of epoch
for (Batch batch : trainer.iterateDataset(testIter)) {
// Update loss and evaulator
EasyTrain.validateBatch(trainer, batch);
batch.close();
}
trainer.notifyListeners(listener -> listener.onEpoch(trainer));
if (epoch % logInterval == 0) {
epochCount[epoch/logInterval - 1] = epoch;
trainLoss[epoch/logInterval - 1] = trainer.getTrainingResult().getEvaluations().get("train_loss");
testLoss[epoch/logInterval - 1] = trainer.getTrainingResult().getEvaluations().get("validate_loss");
}
}
System.out.println("Training complete...");
model.close();
}
```

### 4.4.4.3. Third-Order Polynomial Function Fitting (Normal)¶

We will begin by first using a third-order polynomial function with the same order as the data generation function. The results show that this model’s training error rate when using the testing dataset is low. The trained model parameters are also close to the true values \(w = [5, 1.2, -3.4, 5.6]\).

```
// Pick the first four dimensions, i.e., 1, x, x^2/2!, x^3/3! from the
// polynomial features
int nDegree = 4;
train(polyFeatures.get("0:" + nTrain + ", 0:" + nDegree),
polyFeatures.get(nTrain + ": , 0:" + nDegree),
labels.get(":" + nTrain),
labels.get(nTrain + ":"), nDegree);
```

```
Start Training...
Training complete...
```

```
String[] lossLabel = new String[trainLoss.length + testLoss.length];
Arrays.fill(lossLabel, 0, trainLoss.length, "train loss");
Arrays.fill(lossLabel, trainLoss.length, trainLoss.length + testLoss.length, "test loss");
Table data = Table.create("Data").addColumns(
DoubleColumn.create("epochCount", ArrayUtils.addAll(epochCount, epochCount)),
DoubleColumn.create("loss", ArrayUtils.addAll(trainLoss, testLoss)),
StringColumn.create("lossLabel", lossLabel)
);
Figure figure = LinePlot.create("Normal", data, "epochCount", "loss", "lossLabel");
// set Y axis to log scale
Axis yAxis = Axis.builder()
.type(Axis.Type.LOG)
.build();
Layout layout = Layout.builder("Normal")
.yAxis(yAxis)
.build();
figure.setLayout(layout);
render(figure,"text/html");
```

### 4.4.4.4. Linear Function Fitting (Underfitting)¶

Let’s take another look at linear function fitting. After the decline in the early epoch, it becomes difficult to further decrease this model’s training error rate. After the last epoch iteration has been completed, the training error rate is still high. When used to fit non-linear patterns (like the third-order polynomial function here) linear models are liable to underfit.

```
// Pick the first two dimensions, i.e., 1, x, from the polynomial features
int nDegree = 2;
trainLoss = new double[numEpochs/logInterval];
testLoss = new double[numEpochs/logInterval];
epochCount = new double[numEpochs/logInterval];
train(polyFeatures.get("0:" + nTrain + ", 0:" + nDegree),
polyFeatures.get(nTrain + ": , 0:" + nDegree),
labels.get(":" + nTrain),
labels.get(nTrain + ":"), nDegree);
```

```
Start Training...
Training complete...
```

```
String[] lossLabel = new String[trainLoss.length + testLoss.length];
Arrays.fill(lossLabel, 0, trainLoss.length, "train loss");
Arrays.fill(lossLabel, trainLoss.length, trainLoss.length + testLoss.length, "test loss");
Table data = Table.create("Data").addColumns(
DoubleColumn.create("epochCount", ArrayUtils.addAll(epochCount, epochCount)),
DoubleColumn.create("loss", ArrayUtils.addAll(trainLoss, testLoss)),
StringColumn.create("lossLabel", lossLabel)
);
Figure figure = LinePlot.create("Underfitting", data, "epochCount", "loss", "lossLabel");
// set Y axis to log scale
Axis yAxis = Axis.builder()
.type(Axis.Type.LOG)
.build();
Layout layout = Layout.builder("Underfitting")
.yAxis(yAxis)
.build();
figure.setLayout(layout);
render(figure,"text/html");
```

### 4.4.4.5. Insufficient Training (Overfitting)¶

Now let us try to train the model using a polynomial of too high degree. Here, there is insufficient data to learn that the higher-degree coefficients should have values close to zero. As a result, our overly-complex model is far too susceptible to being influenced by noise in the training data. Of course, our training error will now be low (even lower than if we had the right model!) but our test error will be high.

Try out different model complexities (`nDegree`

) and training set
sizes (`nTrain`

) to gain some intuition of what is happening.

```
// Pick all the dimensions from the polynomial features
numEpochs = 1500;
logInterval = 50;
trainLoss = new double[numEpochs/logInterval];
testLoss = new double[numEpochs/logInterval];
epochCount = new double[numEpochs/logInterval];
train(polyFeatures.get("0:" + nTrain + ", 0:" + maxDegree),
polyFeatures.get(nTrain + ": , 0:" + maxDegree),
labels.get(":" + nTrain),
labels.get(nTrain + ":"), maxDegree);
```

```
Start Training...
Training complete...
```

```
String[] lossLabel = new String[trainLoss.length + testLoss.length];
Arrays.fill(lossLabel, 0, trainLoss.length, "train loss");
Arrays.fill(lossLabel, trainLoss.length, trainLoss.length + testLoss.length, "test loss");
Table data = Table.create("Data").addColumns(
DoubleColumn.create("epochCount", ArrayUtils.addAll(epochCount, epochCount)),
DoubleColumn.create("loss", ArrayUtils.addAll(trainLoss, testLoss)),
StringColumn.create("lossLabel", lossLabel)
);
Figure figure = LinePlot.create("Overfitting", data, "epochCount", "loss", "lossLabel");
// set Y axis to log scale
Axis yAxis = Axis.builder()
.type(Axis.Type.LOG)
.build();
Layout layout = Layout.builder("Overfitting")
.yAxis(yAxis)
.build();
figure.setLayout(layout);
render(figure, "text/html");
```

In later chapters, we will continue to discuss overfitting problems and methods for dealing with them, such as weight decay and dropout.

## 4.4.5. Summary¶

Since the generalization error rate cannot be estimated based on the training error rate, simply minimizing the training error rate will not necessarily mean a reduction in the generalization error rate. Machine learning models need to be careful to safeguard against overfitting such as to minimize the generalization error.

A validation set can be used for model selection (provided that it is not used too liberally).

Underfitting means that the model is not able to reduce the training error rate, while overfitting is a result of the model training error rate being much lower than the testing dataset rate.

We should choose an appropriately complex model and avoid using insufficient training samples.

## 4.4.6. Exercises¶

Can you solve the polynomial regression problem exactly? Hint: use linear algebra.

Model selection for polynomials

Plot the training error vs. model complexity (degree of the polynomial). What do you observe?

Plot the test error in this case.

Generate the same graph as a function of the amount of data?

What happens if you drop the normalization of the polynomial features \(x^i\) by \(1/i!\). Can you fix this in some other way?

What degree of polynomial do you need to reduce the training error to 0?

Can you ever expect to see 0 generalization error?