liveBook · Manning

Overfitting

As explained in Deep Learning with R, Third Edition

Overfitting is a modeling condition in which performance is better on training data than on previously unseen data, including when repeated tuning adapts a model to validation data.

Overfitting happens when a model gets better at its training data without getting better at new data. A handwritten-digit classifier, for example, reached 98.9% training accuracy but 97.8% test accuracy. The goal is not the best training score; it is good generalization. section 2.1 section 5.1

Watch both sides of training

This R/Keras example keeps test images out of training and uses part of the training data for validation: library(keras3) .[.[train_images, train_labels], .[test_images, test_labels]] <- dataset_mnist() train_images <- array_reshape(train_images / 255, c(60000, 28 * 28)) test_images <- array_reshape(test_images / 255, c(10000, 28 * 28)) model <- keras_model_sequential() |> layer_dense(512, activation = "relu") |> layer_dense(10, activation = "softmax") model |> compile(optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = "accuracy") history <- model |> fit(train_images, train_labels, epochs = 20, batch_size = 128, validation_split = 0.2) plot(history) Compare training and validation loss across epochs. If training loss keeps falling while validation loss turns upward, further training is fitting patterns that do not transfer. Choose changes using validation results; reserve `evaluate(model, test_images, test_labels)` for assessing the final model on untouched data. section 2.1 section 4.1.4 section 5.2.1

A practical response depends on where the problem appears:

  • If validation performance improves and then degrades, try fewer epochs. In a movie-review classifier, validation results peaked around epoch four even as training improved. section 4.1.4
  • If the model cannot yet overfit, it may still lack capacity. Add layers, enlarge them, or train longer while monitoring both training and validation metrics. Once it can overfit, refine generalization with regularization or a smaller model. section 6.2.4 section 20.1.5
  • Watch for misleading training patterns: invalid or mislabeled examples, ambiguous labels, and noisy features can draw a model toward behavior that performs poorly on new data. section 5.1.1
  • Do not repeatedly tune against the test set. Even validation results can become misleading when many hyperparameter choices are made from them: information about that set leaks into the configuration. section 5.2.1 section 18.1.1
Figure 5.1 Canonical overfitting behavior
figure

The canonical training curve helps locate the turning point where continued training stops improving generalization. section 5.1.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains the training-versus-validation trajectory, noisy-data risks, deliberate capacity testing, and validation leakage without formal derivations.
Examples
Uses an MNIST training–test accuracy gap and IMDb training and validation curves.
What it assumes
Introduces overfitting with a first neural network, then develops it through classification examples and dataset splits.
What you can do after
Diagnose/debug with it by comparing training and validation metrics, adjusting epochs or capacity, and protecting final test evaluation.
How it connects
Connects optimization, generalization, underfitting, model capacity, regularization, and hyperparameter tuning.

Also explained in

Compare how 24 books explain overfitting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.