Professor Turing

Study guides

What is regularization (L1 and L2)?

Last updated: 2026-11-23

Regularization adds a penalty on the size of a model's weights to the loss the model is fitted to, so the fit gives up a little accuracy on the training data in return for smaller weights. L2 regularization, also called ridge, penalizes the sum of the squared weights. L1 regularization, also called lasso, penalizes the sum of the absolute values of the weights. Both are ways to keep a flexible model from following the accidents of its training data.

Start learning No account needed

1. Regularization in plain words

Fitting a model means choosing weights that make a loss small, usually the squared error between predictions and the data. With many weights and not much data, the loss can be made small by large weights that bend the model around noise. Regularization changes the loss to the data error plus a penalty strength, written lambda, times a penalty on the weights. Now a large weight has to earn its place by reducing the data error enough to pay for itself.

  • L2 (ridge) uses lambda times the sum of squared weights. It shrinks all weights toward zero in proportion to their size, so weights get small without reaching zero.
  • L1 (lasso) uses lambda times the sum of absolute weights. Its pull on a weight stays the same size however small the weight gets, so a weight whose data signal is weaker than that pull sits at exactly zero. That makes L1 a way to drop inputs from the model.

You choose lambda, usually by trying several values and keeping the one with the lowest error on validation data. The reason this helps is in the bias-variance tradeoff: the penalty adds a little bias and removes more variance, which can lower the error on new data.

2. A worked example: one weight, three points

The three data points below are invented for this page. The inputs x are 1, 2 and 3 and the outputs y are 2, 3 and 5. The model predicts y as w times x, with a single weight w and no intercept. The data error is the sum of squared errors, which we write SSE.

  • Sum of x times y: 1 x 2 + 2 x 3 + 3 x 5 = 2 + 6 + 15 = 23.
  • Sum of x squared: 1 + 4 + 9 = 14.
  • No penalty: setting the slope of the SSE to zero gives w = 23 / 14 = 1.64. The errors are 0.357, -0.286 and 0.071, so the SSE is 0.1276 + 0.0816 + 0.0051 = 0.21.

With an L2 penalty the loss is SSE plus lambda times w squared, and the weight that fits is w = 23 / (14 + lambda).

  • Lambda 14: w = 23 / 28 = 0.82, and the SSE rises to 9.66.
  • Lambda 46: w = 23 / 60 = 0.38.
  • Lambda 1000: w = 23 / 1014 = 0.023. The weight is tiny and still not zero.

With an L1 penalty the loss is SSE plus lambda times the absolute value of w. For a positive weight the weight that fits is w = (2 x 23 - lambda) / (2 x 14) = (46 - lambda) / 28, and when that comes out at zero or below, the weight is 0.

  • Lambda 14: w = (46 - 14) / 28 = 1.14, and the SSE is 3.71.
  • Lambda 46: w = (46 - 46) / 28 = 0. All predictions are 0, so the SSE is 4 + 9 + 25 = 38.
  • Lambda above 46: w stays at 0.

At the same lambda of 14, L2 cut the weight from 1.64 to 0.82 and L1 cut it to 1.14, so the two penalties are not interchangeable at one number. Libraries also scale the penalty in different ways, for instance by dividing the data error by the number of points, so the same lambda can mean different strengths in different tools. These numbers come from a short script, regularization-example.mjs in our repository, and a test checks that the page and the script agree.

3. The common mix-up: thinking a bigger penalty is a stronger fix

It is easy to read regularization as a dial where more is safer, because the penalty exists to prevent overfitting. The example shows the cost. At lambda 46 the L1 weight is exactly 0, the model predicts 0 for all inputs, and the SSE on the three points is 38, the same as the error of ignoring x entirely. The model has stopped overfitting because it has stopped learning. A penalty that is too strong gives high bias, and one that is too weak leaves the variance high.

The right strength depends on the data, which is why lambda is chosen on validation data and not set once. Regularization also does not repair a model that is wrong for the problem, such as a straight line for a curved pattern.

4. What regularization does not tell you

  • Penalties act on the size of weights, so inputs measured on different scales are penalized unevenly. Standardize the inputs before you apply L1 or L2, and leave the intercept out of the penalty, which is the usual choice.
  • L1 setting a weight to zero does not mean that input is unrelated to the outcome. When two inputs carry similar information, L1 tends to keep one and drop the other, and which one it keeps can change with the sample.
  • One weight is a toy case. With many weights the pieces interact, and the neat formulas above do not apply, though the pattern of L2 shrinking and L1 dropping carries over.

5. Test yourself on it

Close this page and, for the same three points, work out the L2 weight and the L1 weight at lambda 28, showing each step. Then say which of the two you would use if you suspected that only a few of your inputs matter, and why. Reopen the page and compare, looking for the first step where you slipped, which is often forgetting the factor of 2 in the L1 formula, or using the L2 formula for the L1 penalty.

Roediger and Karpicke had students study prose passages and then either restudy them or take recall tests. After 5 minutes restudying did better. On tests after 2 days and a week, the tested group kept substantially more, even though restudying made students more confident. Their material was prose, not machine learning, so the link to regularization is our suggestion. For the general method, see how to test yourself.

What is the difference between L1 and L2 regularization?

L2 adds the sum of squared weights to the loss and shrinks all weights smoothly toward zero. L1 adds the sum of absolute weights and can set some weights to exactly zero.

What the sources found, and their limits

Roediger and Karpicke, 2006. Two experiments with college students who studied prose passages, then restudied or took one or three free-recall tests with no feedback. After 5 minutes restudying did better; on delayed tests testing gave substantially greater retention. Limits: prose passages in a lab, not machine learning, and we read the abstract record, not the full paper. Source: Europe PMC abstract record, Psychological Science, 2006, read on 2026-11-18.

The definitions and the worked example on this page are standard textbook arithmetic that we computed ourselves, not findings from a study, and the three data points are invented.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.

Bring a topic you are studying, such as why L1 can set a weight to exactly zero. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.

It does not read or mark a written answer, and it does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.

FAQ

What is the difference between L1 and L2 regularization? L2 adds the sum of squared weights to the loss and shrinks all weights smoothly toward zero. L1 adds the sum of absolute weights and can set some weights to exactly zero. In our example at a penalty strength of 46, the L1 weight is 0 and the L2 weight is 0.38.

Does regularization fix overfitting? It can reduce it. A penalty lowers the variance of the fitted model and adds some bias. A penalty that is too strong underfits, as in our example where the L1 penalty at strength 46 raises the squared error on the training points from 0.21 to 38.

Can the tutor mark my explanation of regularization? No. It does not take a written answer to mark. It can ask you questions about what the penalty does to the weights and which type fits your problem, and you write the explanation yourself.

Start with what you're working on

Tell the tutor which topic you are studying and let it ask you the questions.

Start learning No account needed

See also: all study guides