Regularization
Train a flexible model on a small pile of noisy data and something strange often happens to its
weights: they don't stay small and sensible. They swing to huge, wild values —
+50 here, -38 there — each one straining to
bend the curve through every last wiggle and outlier in the training set. That's the fingerprint of
overfitting:
a model so eager to explain every quirk of its training data that it has stopped describing the
underlying pattern at all.
Regularization is a gentle leash on those weights. It doesn't touch the data or the
model's shape — it changes what "success" means during training, by adding a penalty for large
weights straight into the cost function. Instead of minimizing only the error, the model now
minimizes
J = \underbrace{\text{error}}_{\text{fit the data}} + \lambda\underbrace{\lVert\vec{w}\rVert^2}_{\text{keep weights small}}.
The extra term is the squared
length
of the weight vector, scaled by a strength \lambda (the Greek letter
"lambda"). Now every weight faces a trade-off: a big weight is only "worth it" if it buys enough
reduction in error to outweigh the penalty attached to being big. Small, timid weights are free;
huge, wild ones cost something. The result is a smoother, calmer function — one that needs real
evidence in the data before it will commit to a dramatic curve, unless the data really demands that
complexity.
Worked example: same data, two very different weights
Picture fitting a wiggly polynomial to the same handful of noisy points from
the overfitting scenario.
Trained with no penalty at all (\lambda = 0), the fitted weights
might come out looking like
\vec{w}_{\text{unregularized}} = (\,47.2,\ -38.9,\ 52.1,\ -29.4,\ 41.7\,)
— huge numbers of alternating sign, each one fighting the next just to snake the curve through every
training point exactly. Retrain on the very same data with a modest penalty added, and the weights
settle down to something like
\vec{w}_{\text{regularized}} = (\,2.1,\ -1.4,\ 1.8,\ -0.6,\ 0.9\,)
Same data, same model shape — but a squared length in the hundreds shrinks to a squared length under
ten. The regularized curve traces the same broad trend without chasing every last training-set
wiggle, and it usually predicts new points far better, even though it fits the
training points slightly worse. That small sacrifice on data it has already seen is exactly
the point.
It helps to picture the penalty as a household budget. Every weight is a purchase, and
\lambda sets the price per unit of "size." With no budget at all
(\lambda = 0) the model spends without limit, buying an enormous,
finely-tuned weight for every quirk it notices, however tiny. Add a price tag, and it suddenly has to
ask: is this particular wiggle worth paying for? A weight that only exists to chase one noisy point
gets cut first, because a single data point can't justify much spending. A weight that captures a
real, repeated pattern across many points survives, because the error it prevents easily outweighs
its cost. Regularization doesn't decide in advance which weights matter — it simply makes every
weight justify itself.
Turn up the penalty — and go too far
Drag the slider below from \lambda = 0 upward and watch the fitted curve
change in real time. At \lambda = 0 the penalty term vanishes completely
and the cost function is back to plain, unregularized error — you get exactly the wild overfit curve
from before. Nudge \lambda up a little and the curve relaxes, the wiggles
smoothing into the broad trend that actually generated the data.
Now keep going. Push \lambda to a very large value and something you
might not expect happens: the curve doesn't just get smooth, it goes almost flat, ignoring
the data's real shape too. The penalty has become so dominant that the model prefers weights near
zero over almost any amount of fit — and a model with weights near zero can barely represent
anything. You've pushed clean through "just right" and out the other side into
underfitting
— the very problem regularization was meant to cure, now arriving from the opposite direction.
\lambda is not "more is always better"; it is a dial with a sweet spot
somewhere in the middle, and that sweet spot has to be found, not assumed.
You can compute the cost directly for a single weight to see the trade-off in numbers. Suppose a
weight of w = 4 gives an error of 2.0, while
shrinking it to w = 1 raises the error slightly to
2.6 (a slightly worse fit to the training data). With no penalty
(\lambda = 0), the costs are simply J = 2.0 and
J = 2.6 — the big weight wins by fitting better. Turn on a penalty of
\lambda = 0.5: now
J = 2.0 + 0.5 \times 4^2 = 10.0 for the big weight, against
J = 2.6 + 0.5 \times 1^2 = 3.1 for the small one. The small weight now
wins decisively — the penalty made the "cheaper" option worth far more than the tiny gain in fit the
big weight bought.
It's tempting to treat \lambda as a knob you eyeball once and forget.
Don't. It is a hyperparameter, exactly like the ones you already have to tune — the
degree of a polynomial, the depth of a tree, the number of neighbours in a
nearest-neighbour
model. The right way to pick it is to try several candidate values, measure each one's performance
on held-out validation data the model never trained on, and keep the value that
does best there — never by staring at training error, and never by peeking at a test set you plan to
report a final score from.
There's a second trap hiding underneath the first: regularization is a cure for
variance, not for bias. If your model is fundamentally the wrong
shape for the problem — a straight line trying to fit a sharp curve, say — no amount of tuning
\lambda will rescue it. Turning the penalty up on a model that was already
too simple only makes it simpler still, deepening the underfitting rather than curing anything.
Regularization tames a model that is too flexible for its own good; it cannot fix one that was never
flexible enough to begin with.
Two common flavours: L1 and L2
Penalizing the squared length of the weights — the L_2 norm,
\sum_i w_i^2 — is called ridge regression (or "L2
regularization"). It shrinks every weight smoothly toward zero, but rarely pushes one to
exactly zero; every feature keeps some small say in the final answer, just a quieter one.
Penalizing the sum of absolute values instead — the L_1 norm,
\sum_i |w_i| — is lasso regression (or "L1
regularization"). Its penalty has sharp corners rather than a smooth bowl, and that geometry means it
often drives some weights to exactly zero, quietly deleting features from the model
altogether. That side effect has earned its own name: automatic feature selection.
Either way, \lambda stays a
bias–variance
knob you tune on held-out data, and regularization remains one of the most reliable tools for making
models generalize.
- Ridge (L2): minimize
\text{error} + \lambda \sum_i w_i^2 — a smooth bowl-shaped penalty
that shrinks every weight but almost never sets one to exactly zero.
- Lasso (L1): minimize
\text{error} + \lambda \sum_i |w_i| — a penalty with sharp corners at
zero that can push some weights to exactly zero, dropping the corresponding feature
from the model entirely.
- Both reduce to the plain, unregularized cost when \lambda = 0, and
both squeeze every weight toward zero as \lambda \to \infty.
A concrete side-by-side: imagine a model with five candidate features, only two of which actually
matter. Trained with no penalty, all five might end up with sizeable, similarly scaled weights —
the model can't tell the useful features from the noisy ones just by looking at the fit. Add a lasso
penalty and a typical outcome looks like
\vec{w} = (\,3.4,\ 0,\ 0,\ -2.1,\ 0\,) — three features zeroed out
completely, leaving only the two that were pulling their weight. Add a ridge penalty of similar
strength instead, and you'd more likely see
\vec{w} = (\,2.6,\ 0.3,\ -0.2,\ -1.8,\ 0.4\,) — every feature shrunk, none
eliminated. Both models can generalize well; lasso additionally hands you a shortlist of which
features seemed to matter.
"Do not multiply entities beyond necessity" — the principle that, when two explanations both fit the
facts, you should prefer the simpler one — is usually credited to the 14th-century English friar
William of Ockham and is known ever since as Occam's razor. It shows
up everywhere from philosophy to physics as a rule of thumb for choosing between rival theories that
explain the same observations equally well.
Regularization is Occam's razor made mathematically precise and fully automatic. Instead of a human
arguing "prefer the simpler model," the penalty term does the arguing for you: a complicated,
high-weight explanation has to earn its keep by reducing error by more than its complexity costs.
Lasso's habit of deleting features outright is now used deliberately as a discovery tool — in
genomics, where a single study might measure tens of thousands of genes but only a handful truly
drive a disease, lasso regression is a standard way to let the data point at the small set of genes
worth investigating further, out of a haystack of thousands of candidates. Finance analysts do the
same trick on thousands of candidate market signals, letting the zeros do the sorting for them.
Where this shows up in practice
Regularization isn't a niche trick reserved for small toy examples — it's baked into nearly every
serious machine-learning system in some form. Linear and logistic regression libraries default to a
small amount of L2 penalty unless you turn it off. Decision trees and their ensembles use a cousin
idea called pruning — cutting back branches that only exist to memorise a handful
of training examples. Deep neural networks, which can easily have millions of weights, lean on
several regularization tricks at once: a direct weight penalty (often called
weight decay there, though it's the same L_2 idea in
disguise), dropout (randomly switching off a fraction of neurons during each
training step, so the network can't lean too heavily on any one path), and simply stopping training
early once validation performance stops improving. Different mechanisms, same underlying goal: keep
the model from memorising noise it happened to see once.