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
The canonical training curve helps locate the turning point where continued training stops improving generalization. section 5.1.1























