Overfitting and Regularization
Recognize underfitting and overfitting from training and validation results, and use early stopping, dropout, weight decay and augmentation to fix them.
Welcome back!
Step 1 of 15Learn
The student who memorized the answer key
Two students prepare for an exam using last year's practice test. One understands why each answer is right. The other memorizes "question 4: C, question 5: A." On the practice test, both score 100%. On the real exam, with new questions, only the first one does well.
Neural networks face the same choice. A network with 100,000 parameters trained on 1,000 examples has more than enough capacity to memorize every example, including its noise and quirks, rather than learning the general pattern. This is overfitting: excellent performance on training data, much worse performance on new data. The gap between the two is the warning sign.
The opposite problem is underfitting: the model is too simple, or trained too briefly, to capture the pattern even on the training data. Both scores are poor.
| Training accuracy | Validation accuracy | Diagnosis |
|---|---|---|
| 72% | 70% | Underfitting |
| 94% | 92% | Good fit |
| 99.8% | 81% | Overfitting |
The aim of training isn't a low training loss. It's generalization: good performance on data the model has never seen.
Step 2 of 15Practice · Sort into groups
Diagnose each training result.
Tap an item, then tap its group (or drag it there).
Step 3 of 15Diagram
Reading training and validation scores together
Tap a shape with to see more.
- Overfitting
Great on training data, much worse on new data: the model memorized.
- Good fit
Both high with a small gap. This is the goal.
- Underfitting
Poor everywhere: the model is too small, trained too briefly, or the features are weak.
- Suspicious: check for bugs
Validation clearly better than training usually means leakage, a mislabeled split, or heavy regularization only active in training.
Step 4 of 15Learn
Learning curves and early stopping
Plot the training and validation loss after every epoch and a typical pattern appears. At first, both fall together: the network is learning real patterns that hold for all data. Then the validation loss flattens and starts to rise, while the training loss keeps falling. From that point on, the network is learning details specific to the training examples. Each extra epoch makes it better at the training set and worse at everything else.
The simplest remedy follows directly: early stopping. Keep a copy of the weights from the epoch with the lowest validation loss, keep training for a few more epochs (the patience, say 5) to make sure it isn't a temporary bump, and if no improvement comes, stop and restore the best weights.
Early stopping is cheap, needs no change to the model, and is used in a huge number of training scripts. It does depend on a trustworthy validation set, which is one more reason to keep that set separate from the training data and representative of the real data the model will face.
Step 5 of 15Practice · Choose one
Validation loss is lowest at epoch 12 and rises steadily after that, while training loss keeps falling until epoch 40. Which weights should you keep?
Step 6 of 15Learn
Regularization: making memorization harder
Regularization is any technique that discourages the network from fitting noise. The main tools:
More data. The most reliable fix. Memorizing 1,000 examples is easy; memorizing 1,000,000 is much harder, so the network is pushed toward general patterns.
Data augmentation. When more data is expensive, create variations of what you have: flip, crop, rotate or slightly recolor images; add noise to audio. A cat flipped horizontally is still a cat, so the label stays valid, and the network learns to ignore details that don't matter.
Weight decay (L2 regularization). Add a penalty to the loss for large weights: total loss = data loss + λ · Σ w2. The network can still use big weights where they really help, but every weight has to "pay" for itself. Smaller weights produce smoother functions that are less able to bend around individual points. λ is a hyperparameter, often something like 0.0001 to 0.01.
Dropout. During training, randomly switch off a fraction of neurons (often 20–50%) on every forward pass. No neuron can rely on any specific partner being present, so the network learns redundant, robust features. At inference time, dropout is turned off and all neurons are used, with outputs scaled so their expected size matches training.
A smaller model. Fewer layers or neurons means less capacity to memorize, though too small a model will underfit.
Step 7 of 15Practice · Match the pairs
Match each regularization technique to how it works.
Tap an answer, then tap the place where it belongs (or drag it there).
- Dropout
- Weight decay
- Data augmentation
- Early stopping
Step 8 of 15Flashcards
Regularization toolkit
Tap a card to flip it.
Step 9 of 15Practice · Work it out
A model's data loss is 0.40. It uses L2 weight decay with λ = 0.01, and (to keep it simple) has just two weights, 3 and −4. What is the total loss, data loss + λ · Σ w2?
Step 10 of 15Note
Step 11 of 15Learn
When accuracy lies
Generalization also depends on measuring the right thing. Imagine a fraud detector where 1% of transactions are fraudulent. A "model" that always says "not fraud" scores 99% accuracy while catching zero fraud. On imbalanced data, accuracy hides failure.
Better metrics come from the four counts of a binary classifier: true positives (TP, fraud flagged correctly), false positives (FP, honest transactions flagged), false negatives (FN, fraud missed) and true negatives (TN).
- Precision = TP / (TP + FP): of everything flagged, how much was really fraud?
- Recall = TP / (TP + FN): of all real fraud, how much was caught?
- F1 score: the harmonic mean of precision and recall, a single number that is high only when both are.
The two pull against each other through the decision threshold. Lower the threshold and you catch more fraud (higher recall) but annoy more honest customers (lower precision). Which matters more depends on the cost of each mistake: in cancer screening, missing a case is worse than a false alarm, so recall wins; for a spam filter that might hide a job offer, precision matters more.
Step 12 of 15Chart
Why 99% accuracy can mean nothing
In the fraud example, 1 transaction in 100 is fraud. A model that always says "not fraud" is right on the whole big slice and wrong on every case that matters.
| Item | Share of transactions |
|---|---|
| Legitimate | 99% |
| Fraud | 1% |
Step 13 of 15Practice · Work it out
A fraud model flags 100 transactions. 80 of them are real fraud (true positives) and 20 are honest (false positives). It also misses 40 fraudulent transactions (false negatives). What is its recall, in percent? Give one decimal place.
Step 14 of 15Practice · True or false
Dropout stays switched on when a trained model makes predictions for users, so its answers include some randomness.
Step 15 of 15Practice · What would you do?
What would you do?
A plant-disease classifier trained on 3,000 leaf photos reaches 98% training accuracy but only 71% validation accuracy. Which next step is most likely to help?
Lesson complete!