A model overfits when it fits the training data better than the patterns in that data justify. It learns details that are specific to the training examples, so its performance on new data is worse than its performance on the data it was trained on. This article shows how to see overfitting in the training and validation curves, what dropout, weight decay and early stopping do in PyTorch, and how much each of them changed the result in a measured experiment.
The setup
The experiment uses the 8x8 digits dataset from scikit-learn: 1,797 grayscale images of the digits 0 to 9, each flattened to 64 values. 797 images are kept aside as the validation set and are never trained on. The model is trained on only 100 images, which is small enough to overfit easily.
import torch import torch . nn as nn from sklearn . datasets import load_digits
d = load_digits ( ) X = torch . tensor ( d . data , dtype = torch . float32 ) / 16.0 # pixels 0..16 -> 0..1 Y = torch . tensor ( d . target ) perm = torch .β¦