Machine Learning / Training & Evaluation
Keeping a model from memorizing noise.
Reviewed by Yuvaraj
A model that has overfit has essentially memorized its training data instead of learning the pattern behind it. It nails every example it was trained on, then stumbles on anything new. The clearest symptom is a widening gap between two numbers you should always track separately: training error and validation error. When training error keeps falling while validation error flattens or starts climbing, the model is spending its extra capacity fitting noise rather than signal. Regularization is the family of techniques that pushes back on this, it deliberately constrains the model so it prefers simpler explanations that generalize.
Overfitting is diagnosed by comparison, not by a single number. A tiny training error means nothing on its own; you have to hold out data the model never sees during fitting and watch how the two errors move together.
| Signal | Underfitting | Good fit | Overfitting |
|---|---|---|---|
| Training error | High | Low | Very low |
| Validation error | High | Low | High |
| Gap (val minus train) | Small | Small | Large |
A large, growing gap is the tell. The model is fitting quirks specific to the training set, sampling noise, outliers, spurious correlations, that will not repeat on new inputs.
The core idea is to stop optimizing raw fit and instead optimize fit plus a penalty on model complexity. For a loss and weight vector , the regularized objective is:
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
Here is the squared L2 norm (this is ridge regularization). The hyperparameter controls the trade-off:
So trades fit against simplicity: it is the dial between low bias / high variance and high bias / low variance. The L1 (lasso) variant swaps the penalty for , which behaves quite differently.
Penalties are only one lever. For most models, and especially neural networks, combine several remedies.
Fit the same three-feature regression two ways. The unregularized model minimizes raw MSE and lands on large weights; the ridge model () pays a penalty for size.
import numpy as np
w_plain = np.array([8.0, -6.5, 5.2]) # unregularized fit
w_ridge = np.array([1.9, -1.2, 0.8]) # ridge, lambda = 0.1
lam = 0.1
lam * np.sum(w_plain**2) # 0.1 * 133.29 = 13.33
lam * np.sum(w_ridge**2) # 0.1 * 5.69 = 0.57
| Model | Sum of w squared | Penalty () | Train MSE | Val MSE |
|---|---|---|---|---|
| Unregularized | 133.29 | 13.33 | 0.02 | 0.95 |
| Ridge () | 5.69 | 0.57 | 0.15 | 0.30 |
The unregularized model drives training MSE almost to zero using big weights, but validation MSE is a dismal 0.95, textbook overfitting. Once the penalty is active, those large weights cost 13.33 in the objective, so the optimizer settles for smaller ones. Training MSE rises slightly to 0.15, yet validation MSE falls to 0.30, and the train/val gap shrinks from 0.93 to 0.15. That is regularization working: a little worse on the training set, much better on new data.
Why smaller weights generalize
Large weights let a model produce sharp, wiggly outputs that pass exactly through noisy training points. Shrinking them forces smoother functions, which are less able to encode noise and more likely to capture the true signal.
Common mistakes