When the Model Memorizes (Overfitting)
How to notice that the model didn't learn but memorized.
Your ML model scores 99% on training data — but crashes to 55% on new data. The model didn't learn to solve the problem. It memorized the answers. This is called overfitting, and it's the single most common failure mode in machine learning.
In this article, you'll learn why overfitting happens, how to detect it, and which tools help you avoid it.
The Memorizing Student — What Is Overfitting?
Overfitting
Unlike a student, an ML model has no 'understanding' — it optimizes a loss function. The analogy works for intuition but shouldn't suggest that models have cognitive processes.
Example: Polynomial Fitting
Myth: High training accuracy = good model
Every very complex model (like today's large AI systems) tends to memorize data. This is why developers monitor both training loss and validation loss. When validation loss starts rising while training loss keeps falling, you're overfitting — and that's when you stop training (Early Stopping).
Underfitting
Symptom: Low training error, high test error. Cause: Model too complex or trained too long. Solution: Simplify model, more data, regularization, early stopping.
Symptom: High training error AND high test error. Cause: Model too simple or trained too little. Solution: More complex model, more features, train longer.
Interactive: Underfitting vs. Overfitting
Move the slider to see how an underfitted model (left) differs from an overfitted model (right). The sweet spot is in the middle — the model captures real patterns without memorizing noise.
Underfitting vs. Overfitting
Move the slider to switch between underfitting (left) and overfitting (right). The blue dots are training data. The curve shows how the model interprets the data.
Underfitting
The model is too simple. It doesn't even recognize the obvious patterns in the training data. Like a student who hasn't understood the task.
Overfitting
The model is too complex. It memorizes every single data point, including noise. Like a student who memorizes answers instead of understanding.
The optimal compromise lies in the middle: complex enough to recognize real patterns, but simple enough to generalize to new data. Techniques like regularization, cross-validation, and early stopping help find this point.
The U-Shaped Curve — Finding the Sweet Spot
Imagine a chart: model complexity (e.g., polynomial degree) on the X-axis, error on the Y-axis. What emerges?
Training error decreases steadily with increasing complexity — the model fits the training data ever more closely. But test error behaves differently: it drops first (as the model improves), then rises again (as the model starts learning noise).
This U-shaped test curve is the central diagnostic tool. At the lowest point of the U lies the optimal point — the best balance between too simple (underfitting, left) and too complex (overfitting, right).
Deep Dive: The Bias-Variance Tradeoff
Train, Validate, Test — The Three-Exam System
How do you know if your model generalizes? You test it on data it has never seen. For this, the dataset is split into three disjoint parts:
Like school: homework is for practice, the mock exam shows where you stand, and the final exam can only be taken once. Anyone who peeks at the real exam and studies from it no longer has a valid result.
The golden rule: Test data used only ONCE
Practice: Splitting data with scikit-learn
For small datasets, a single split is not enough. Cross-validation helps: the dataset is divided into k parts ('folds'), and training is repeated k times, with each fold serving as validation once.
What Can You Do?
Overfitting is not inevitable. These four strategies help:
Dropout Regularization
Geoffrey Hinton, Nitish Srivastava, Alex Krizhevsky, Ilya Sutskever, and Ruslan Salakhutdinov substantially changed the training of neural networks in July 2012 with the invention of dropout regularization. This elegant technique prevents overfitting by randomly disabling roughly half of all neurons during training, thereby avoiding complex co-adaptations. Instead of learning specific feature combinations, each neuron learns robust, broadly useful recognition patterns. Published on arXiv on July 3, 2012, the method became, a few months later, one of the building blocks of AlexNet's ImageNet triumph at ILSVRC 2012 — whose results were presented in October 2012 — alongside GPU training, ReLU activation, and network depth, and became the standard in most modern deep learning architectures. Dropout set new records in speech and object recognition and solved the central overfitting problem of deep networks.
Interactive: Why Complexity Explodes
Move the slider to see how fast the computational costs of different model complexities grow. An O(n²) model already needs 10,000 operations at 100 data points — a key reason why regularization matters.
At n=100, the difference becomes visible: O(n²) requires 10.000 operations, while O(n) needs only 100. O(log n) needs just 6.6 — that's 15x less than O(n).
Ratio to O(n)
| Complexity | Operations | Factor vs. O(n) |
|---|---|---|
| O(1) | 1 | 100x faster |
| O(n) | 100 | 1x (Reference) |
| O(n²) | 10.000 | 100x slower |
| O(2ⁿ) | 1.073.741.824 | 10737418x slower |
Summary
Quiz: Overfitting
Understanding Check
- Why is high training accuracy alone a dangerous metric?
- What is the difference between validation set and test set — and why can the test set only be used once?
- What happens to bias and variance when you increase model complexity?