liveBook · Manning

Overfitting

As explained in Learn Generative AI with PyTorch

A training failure mode where the model memorizes training data too closely and generalizes poorly to new inputs.

Overfitting occurs when a model performs well on training data but poorly on unseen inputs. The model has learned the training examples too closely instead of generalizing. section 2.2.1 section 2.4.1

Training loss alone cannot identify the best model. A validation set supports hyperparameter selection and reveals when additional training stops improving generalization. section 2.2.1 section 2.4.1

A practical training workflow uses separate data and a stopping rule:

  • Divide the original training data into a training set and a validation set; one example uses 50,000 and 10,000 observations. section 2.4.1
  • Load training, validation, and test sets separately with PyTorch DataLoader objects. section 2.4.1
  • Track validation loss while training and stop after it fails to improve for a chosen patience period; the example uses patience=10. section 2.4.1
  • Evaluate the trained model on the test set after selecting when to stop. section 2.2.1

A GPT model memorizing passages

A decoder-only GPT model is trained to predict the next token. Training for too long can make its generated text identical to passages in the novels used for training. Comparing checkpoints saved every 10 epochs shows the tradeoff: the 40-epoch model is highly coherent but may copy passages, while the 20-epoch model is less likely to copy but can contain grammatical errors. section 12.1.2 section 12.0

Figure 2.1 The steps involved in training a deep learning model

Use the training-process figure to follow prediction, loss calculation, backpropagation, and weight updates as the model learns. section 2.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines the failure mode, demonstrates its effects, and gives validation-loss early stopping, but does not develop broader regularization methods.
Examples
Demonstrates image classification with train-validation-test splits and GPT text generation that copies training passages.
What it assumes
Assumes basic model training and loss concepts while introducing validation splits, epochs, hyperparameters, and test evaluation.
What you can do after
Diagnose overfitting with validation loss and control it by selecting checkpoints or stopping training after a patience interval.
How it connects
Connects overfitting to validation sets, hyperparameters, early stopping, training loss, test sets, and cross-entropy loss.

Also explained in

Compare how 24 books explain overfitting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.