← Writing

basics · English · 7 min

🇻🇳 Đọc tiếng Việt

What Is Overfitting? When AI Memorizes Instead of Learning

January 5, 2026

Overfitting happens when a model learns the training data so thoroughly that it can't generalize to new data. It's the most fundamental problem in machine learning — and the reason it happens mirrors the most common way humans study wrong.

Imagine a student preparing for an exam. Instead of understanding the material, they memorize every question that appeared on every past exam — word for word, answer for answer.

The result? If the exam is identical to the past ones, perfect score. If the exam changes even slightly — that student fails.

That's exactly what overfitting is in machine learning.

Memorizing vs. learning

When you train an AI model, you feed it a sample dataset — called training data. The goal is for the model to learn general patterns it can apply to new, unseen data.

But sometimes a model learns too thoroughly. Instead of learning the underlying rules, it memorizes the specific details, noise, and exceptions in the training data.

The result: accuracy on training data is very high, but on new data it's poor.

The doctor example

A medical AI is trained only on patient records from one small urban hospital. The model learns the specific characteristics of those patients — their age range, ethnicity, local climate, regional diet.

When deployed to diagnose patients in another region, it fails because it doesn't recognize different patterns. The model didn't learn the nature of the disease — it learned the specific features of one patient population.

Why does overfitting happen?

Two main causes:

1. The model is too complex for the data. A model with too many parameters has enough "capacity" to memorize each data point rather than find general patterns. Like using a supercomputer to cram for a quiz.

2. Training data is too small or insufficiently diverse. With only 100 examples to learn from, a model can memorize those 100 examples instead of learning universal rules.

How to spot it

The simplest way to detect overfitting: compare performance on training data vs. test data.

  • Training accuracy: 98%
  • Test accuracy: 62%

A large gap = overfitting. The model works well on data it has already "seen," but fails on new data.

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
 
# Generate sample data
X, y = make_classification(n_samples=300, n_features=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
 
# Try three different levels of model complexity
for depth in [2, 8, 50]:
    model = DecisionTreeClassifier(max_depth=depth, random_state=42)
    model.fit(X_train, y_train)
 
    train_acc = model.score(X_train, y_train)
    test_acc  = model.score(X_test,  y_test)
 
    status = "✓ good" if abs(train_acc - test_acc) < 0.1 else "✗ overfitting"
    print(f"depth={depth:2d} | train={train_acc:.2f} | test={test_acc:.2f} | {status}")
 
# Output:
# depth= 2 | train=0.84 | test=0.83 | ✓ good
# depth= 8 | train=0.96 | test=0.85 | ✓ good
# depth=50 | train=1.00 | test=0.74 | ✗ overfitting
Model complexityTrain accuracyTest accuracyVerdict
Too simple~80%~79%Underfitting — hasn't learned enough
Just right~92%~87%Good — generalizes well
Too complex100%~74%Overfitting — memorized the data

How to fix it

No silver bullet, but some common approaches:

  • More data: More diverse training data helps the model learn general patterns rather than specific details.
  • Regularization: Techniques that "penalize" model complexity — forcing it to stay simple and not over-rely on any particular feature.
  • Dropout (in deep learning): Randomly disabling neurons during training so the model can't depend too heavily on any single pathway.
  • Cross-validation: Testing the model on multiple different data subsets to ensure it's learning generalizable rules.

What this means in practice

Overfitting is why an AI that performs brilliantly in the lab often fails in production. Real users, real data — always different from what the model trained on.

It's also why evaluating a model on data it has never seen is the most fundamental principle in machine learning. Without that, you don't actually know what you're measuring.

Common Mistakes

  • Only watching training accuracy. If you don't track test accuracy in parallel, overfitting can happen silently. Always log both at the same time.
  • Splitting train/test too late. Many people split data after preprocessing or feature selection — by then, test information has already "leaked" into decisions. Split immediately, keep the test set locked until final evaluation.
  • Using the test set to pick the best model. If you train 10 models and pick the one with the best test accuracy, you've overfitted to the test set. Use a validation set for selection; test set is for the final report only.
  • Assuming regularization will fix everything. Regularization helps — but it doesn't replace having sufficient and diverse training data. Systematic bias in training data can still cause subtle overfitting.
  • Ignoring distribution shift. The model doesn't overfit on your test set but still underperforms in production because real-world data differs from both train and test. This "silent overfitting" is the most dangerous kind and only appears through production monitoring.

Key takeaways:

  • Overfitting happens when a model memorizes training data details instead of learning general rules
  • The tell: high training accuracy with significantly lower test accuracy
  • Root causes: model too complex, or training data too small / not diverse enough
  • Fixes: more data, regularization, dropout, early stopping, cross-validation
  • The non-negotiable rule: always evaluate on data the model has never seen — no exceptions
machine-learningbasicsbeginner