Machine learningLoading progress…

L2 regularization

Compare model flexibility, regularization and validation procedures without using the test set for tuning.

Why big weights are a warning sign

Look at the overfitted curve in the last lesson. To bend sharply between 12 points a degree-9 polynomial needs huge coefficients that nearly cancel each other. Without any penalty, the squared size of the non-intercept coefficients is 4.2e5 for this data; at the best penalty it is 32. Wiggly means large weights. So we change what the model is trying to minimize:

J(w) = MSE(w) + lambda * (w1^2 + w2^2 + ...)

The first term wants a good fit. The second term charges for the squared size of the weights, and lambda (a non-negative number you choose) says how expensive that is. Lambda = 0 is the old model. A huge lambda forces every weight to 0 and leaves a flat line. The usual convention, and the one we use, is not to penalize the intercept: shifting the whole prediction up or down is not a sign of wiggliness.

MSE (fixed for this demo)0.1
squared L2 norm, w1² + w2²5
L1 norm, |w1| + |w2|3
L2 objective, MSE + lambda · squared L21.1
L1 objective, MSE + lambda · L10.7

With w = [2, -1] and lambda = 0.2 the L2 objective is 0.1 + 0.2 · 5 = 1.1 and the L1 objective is 0.1 + 0.2 · 3 = 0.7. They are different objectives, not two scores of one model to compare.

Check the arithmetic. With w = [2, -1] the squared L2 norm is 5, and the objective is MSE + lambda times 5.

There is a closed form

For squared error plus an L2 penalty you do not need an iterative search. Setting the slope of J to zero gives w = (X^T X + lambda D)^-1 X^T y. Step through a tiny example:

Step 1 of 4
Data. Three points on a line, x = 0, 1, 2 and y = 1, 3, 5. Each row of X starts with a 1 for the intercept: X = [[1,0],[1,1],[1,2]]. Without a penalty the answer is w = [1, 2], the line y = 1 + 2x.

A useful side effect: even when X^T X is singular (two columns that are copies of each other), X^T X + lambda D is invertible for lambda above 0. Ridge stays well defined where plain least squares has no unique answer. Note also that the answer changes with the data, as it should: with y = 1, 3, 6 instead, the slope goes from 2.5 at lambda 0 to 1.667 at lambda 1.

Sweep lambda

degree 9 polynomial, 12 training points
-1-0.500.51-1.5-1-0.500.511.501e-4.51e-2.51e-0.51e1.5-99999-3150+315+99999coefficients (signed log scale)01e-4.51e-2.51e-0.51e1.51e-21e-11besttrainingvalidation
training error0.018validation error3.042
squared size of the coefficients (not counting the intercept)4.2e5
best lambda on this grid1e-3.5 (validation error 0.093)
Slide lambda from 0 to large. Left: the fitted curve. Middle: every coefficient shrinks (signed log scale). Right: the U-shaped validation error with the best lambda circled.

The picture is the mirror image of the degree sweep: strong regularization on the right behaves like a stiff model (underfitting), none on the left behaves like an over-flexible one. The best lambda here is 1e-3.5, with a validation error of 0.093 against 3.04 unregularized. Pick lambda the way you pick degree: by validation error, never by training error (which always prefers lambda = 0).

Predict first. In the path plot, what does a coefficient do as lambda increases?

Scale matters

The penalty adds up squared weights, so it treats a weight of 2 the same whether it multiplies a number in metres or in centimetres. But the weight itself depends on the units: if a length is stored in centimetres instead of metres, the fitted weight is 100 times smaller (0.02 instead of 2), so its penalty is 10 000 times smaller (0.0004 instead of 4) and the same lambda barely touches it.

Predict first. Two features carry the same information, but one is stored with numbers 100 times larger (centimetres instead of metres). With the same lambda, which is shrunk more by ridge?

Chapter context

A model should capture reusable structure without chasing accidental patterns in the training sample. Model selection compares this tradeoff using validation data that resembles future use.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

A possible objective is

J(w)=MSE⁡(w)+λ∑jwj2.J(w)=\operatorname{MSE}(w)+\lambda\sum_jw_j^2.

For L1 replace the squared-weight sum by ∑j∣wj∣\sum_j|w_j|. Here lambda is nonnegative and the bias convention must be specified. Different libraries normalize objectives differently.

Worked example

If MSE=0.1, w=[2,−1] and lambda=0.2, the stated L2 objective is 0.1+0.2×5=1.1. The corresponding L1 objective is 0.1+0.2×3=0.7. These values come from different objectives and should not be compared as if they were the same validation metric.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
w, mse, penalty = [2,-1], .1, .2
assert isclose(mse + penalty*sum(x*x for x in w), 1.1)
assert isclose(mse + penalty*sum(abs(x) for x in w), .7)
assert isclose(sum([.7,.8,.9])/3, .8)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Cross-validation does not repair leakage introduced before folding. More regularization can increase bias. A chosen validation result is not an unbiased final test result.