AI glossary

Overfitting

Overfitting is when a machine learning model learns the training data too closely, including its noise and random fluctuations, so it performs well on the data it was trained on but poorly on new, unseen data. This phenomenon occurs because the model captures not just the underlying signal but also the random variance specific to that particular dataset, leading to poor generalization.

You can visualize this by imagining a student who memorizes every question and answer from a practice test verbatim. When shown the exact same questions on the final exam, they score perfectly. However, when presented with a slightly different set of questions that test the same concepts, they fail because they never actually learned the underlying principles. In machine learning, this distinction between memorization and learning is critical for building models that work in production.

How to detect overfitting

The clearest sign is a gap between training performance and validation/test performance: training accuracy (or loss) keeps improving while validation accuracy (or loss) stalls or gets worse. In practice, this is monitored by plotting training loss and validation loss over training epochs. Overfitting is visible as the point where the two curves diverge, with validation loss starting to increase while training loss keeps falling.

To get a more reliable estimate of whether a model generalizes rather than just memorizing one particular split, you should use Cross-validation. This technique splits the data into multiple train/validation folds and averages performance across them, providing a sturdier signal than a single hold-out set. If the validation score remains consistently lower than the training score, your model is likely overfitting.

Common causes

Several factors contribute to this issue. The primary driver is often that the model is too complex relative to the amount of available training data. When a model has too many parameters or too much capacity for the signal actually present in the data, it has the flexibility to fit the noise.

Other common causes include training for too many epochs. If the model has enough passes over the data, it will eventually start fitting noise rather than the underlying pattern. Insufficient regularization also plays a role; without penalties in the training objective, nothing discourages unnecessary model complexity. Additionally, if the training data is not representative of the real-world distribution or the dataset is too small, the model cannot learn a pattern that generalizes.

How to prevent overfitting

Preventing overfitting involves balancing model capacity with data quality and training duration. Regularization techniques such as L1 (lasso) and L2 (ridge) penalties add a term to the loss function that discourages large or unnecessary model weights. This forces the model to keep weights small, promoting simpler solutions that generalize better.

In neural networks, dropout is a common technique where a fraction of neurons is randomly disabled during each training step. This prevents the network from relying too heavily on any single pathway, encouraging redundancy across the network. Another effective strategy is early stopping, which halts training once validation performance stops improving, before the model has a chance to overfit further.

You can also address the root cause by using a simpler model or collecting more Training data. More data helps the model distinguish between signal and noise. Furthermore, Data augmentation artificially expands the training set by applying transformations to images or text, ensuring the model sees more variation. Finally, using Cross-validation helps select Hyperparameter tuning choices, such as model complexity or regularization strength, that generalize well rather than ones that happen to fit one specific validation split.

Overfitting vs underfitting

Understanding the difference between overfitting and underfitting is essential for diagnosing model performance. Underfitting occurs when the model is too simple to capture the underlying pattern in the data at all, performing poorly on both training and validation data. This results in high training error and high validation error.

In contrast, overfitting is characterized by low training error but high validation error. The model fits the training data (including its noise) very well but generalizes poorly. This trade-off between a model too simple to capture real patterns and one so complex it fits noise is described as the Bias-variance tradeoff: underfitting corresponds to high bias, while overfitting corresponds to high variance. If your model is underfitting, you may need a more complex architecture or better features; if it is overfitting, you need more data, regularization, or early stopping.

A simple example

Consider fitting a high-degree polynomial curve to a small set of noisy data points. A high-degree polynomial has enough flexibility to pass exactly through every training point, achieving zero training error. However, the curve wiggles wildly between points to hit every noise artifact. When you introduce new data points that follow the general trend but not the exact noise, the high-degree curve predicts poorly.

A simpler, lower-degree curve might not pass through every single training point, resulting in a small non-zero training error. Yet, it captures the general trend and generalizes much better to new data. This illustrates why minimizing training error alone is not the goal; the goal is minimizing generalization error.

FAQ

What is the main sign of overfitting?

The main sign is a significant gap between training and validation performance. Specifically, training loss continues to decrease while validation loss begins to increase.

How do you fix an overfitted model?

You can fix an overfitted model by reducing its complexity, adding regularization (like L1 or L2), using dropout, applying early stopping, or increasing the amount of training data.

What is the difference between overfitting and underfitting?

Overfitting means the model is too complex and memorizes noise, leading to high variance. Underfitting means the model is too simple and fails to capture patterns, leading to high bias.

Underfitting Regularization Cross-validation Training data Validation data Hyperparameter tuning Bias-variance tradeoff Data augmentation