L1 and L2 Regularization Explained: What They Do and When to Use Each

L1 regularization vs L2: the penalty each adds to the loss, why L1 gives sparse weights while L2 shrinks them, and when to use L1, L2 or Elastic Net.

L1 and L2 Regularization Explained: What They Do and When to Use Each
Written by TechnoLynx Published on 11 Jul 2026

L1 and L2 regularization both add a penalty on weight size to the training loss, and they differ in what that penalty does to the weights. L1 adds lambda times the sum of the absolute values of the weights, while L2 adds lambda times the sum of their squares. The practical difference in L1 regularization vs L2 comes down to this: L1 pushes weights toward exactly zero, encouraging a sparse model, while L2 penalises weights without making them sparse. Use L2 as the default, and use L1 when you suspect many inputs are irrelevant and want the model to drop them. If you train with Adam, there is one more thing to know: L2 regularization and weight decay stop being the same thing there, which is covered at the end.

The L1 and L2 Regularization Terms, and How Each Enters the Loss

In Keras, regularization penalties on layer parameters or activity are summed into the loss function that the network optimises. The two standard terms are:

  • L1 penalty: loss = l1 * reduce_sum(abs(x)), proportional to the L1 norm of the weights.
  • L2 penalty: loss = l2 * reduce_sum(square(x)), proportional to the squared L2 norm of the weights.

Keras also ships a combined L1L2 regularizer that applies both penalties at once, each with its own factor.

Concretely, l2(0.001) on a layer means every coefficient in that layer’s weight matrix adds 0.001 times its squared value to the total loss. The full objective is the data loss plus lambda times the complexity term, where lambda is the regularization rate that sets how much the penalty counts.

You will often see the L2 regularization formula written with a factor of one half, as ½λw² per weight; the half is there so the gradient comes out as λw rather than 2λw. Scikit-learn’s Ridge, for example, minimises ‖y − Xw‖²₂ + α‖w‖²₂, with alpha as the constant multiplying the L2 term.

How a Penalty on Weight Size Prevents Overfitting

Overfitting is a model fitting noise in the training data, and one common way to limit it is to constrain the network’s complexity by forcing its weights to take only small values, done by adding a cost for large weights to the loss. The optimiser now minimises loss plus complexity, so every large weight carries a cost. Weights that only help memorise individual examples rarely pay their way.

The regularization rate controls that trade. A high rate strengthens the penalty and reduces the chance of overfitting, while a low rate weakens it and raises that chance. Rate trade-offs like this also shape autonomous agents and multiagent systems that learn policies through trial and error. In regression terms, scikit-learn’s documentation says the penalty improves the conditioning of the problem and reduces the variance of the estimates; Zou and Hastie (2005) describe ridge regression as gaining prediction performance through a bias-variance trade-off.

Too much penalty fails the other way. Google’s Machine Learning Crash Course notes that learning rate and regularization rate pull weights in opposite directions: if the regularization rate is high relative to the learning rate, the weak weights produce a model that predicts poorly, and if the learning rate is high relative to it, the strong weights produce an overfit model. Lambda (or alpha) is tuned on held-out data; Ng (2004), for example, held out 30% of the data for selection.

Why Does L1 Produce Exactly Zero Weights While L2 Only Shrinks Them?

L1 produces exact zeros because its push toward zero has a constant size, while L2’s push gets smaller as the weight gets smaller. There are three ways to see this, and they all point to the same thing.

The Gradient View

The gradient of the L2 term is proportional to the weight itself, so every gradient step decays each weight linearly toward zero, W += -lambda * W. As a weight approaches zero its penalty gradient vanishes too, which is why the TensorFlow tutorial says L2 does not make weights sparse: the penalty goes to zero for small weights.

The L1 gradient is λ·sign(w): a constant with the sign of the weight, not something that scales with it. Each update subtracts the same fixed amount from a positive weight (or adds it to a negative one) regardless of how small the weight already is, and that constant push is much more effective at driving weights to exactly zero.

The Soft-Thresholding View

Scikit-learn’s lasso documentation uses the soft-thresholding function S(z, α) = sign(z)·max(0, |z| − α), which is exactly zero whenever the magnitude falls below the threshold. Tibshirani (1996) puts it directly: ridge regression scales the coefficients by a constant factor, whereas the lasso translates them by a constant amount and truncates at zero.

The Geometry View

Written as a constraint, L1 confines the coefficients to a rotated square (a diamond in two dimensions), and the lasso solution is the first point where the elliptical loss contours touch that square, which will sometimes be a corner where one coefficient is zero. The L2 constraint region is a circle with no corners for the contours to hit, so zero solutions rarely occur. Tibshirani also notes that q = 1 is the smallest exponent in the Lq family that still gives a convex region, and Zou and Hastie cite Fan and Li (2001) as proving that within the Lq family with q ≥ 1 only the lasso penalty produces sparse solutions.

Worked Example: One Ridge Step and One Lasso Step on Two Coefficients

Zou and Hastie (2005) give closed-form solutions for an orthogonal design: ridge returns β_OLS / (1 + λ₂), and lasso returns (|β_OLS| − λ₁/2)₊ · sign(β_OLS).

Coefficient (OLS) Ridge, λ₂ = 1: divide by 2 Lasso, λ₁ = 1: subtract 0.5, floor at 0
3.0 1.5 2.5
0.4 0.2 0.0

Ridge halves both coefficients and keeps both in the model. Lasso takes the same absolute amount off each, so the large coefficient keeps more of its size and the small one is removed entirely. That is the whole contrast in two rows.

What L1 Regularization Is Used For

L1 regularization is used for feature selection: because the L1 penalty yields sparse models, it can be used to select features, as scikit-learn documents. When a weight connecting an input to the network becomes zero, that input has no influence along that path. Tibshirani notes that the zeros give interpretable models, which ridge regression does not.

The theoretical case for L1 is strongest when there are many irrelevant features. Ng (2004) proved L2-regularized logistic regression is rotationally invariant, and his results suggest such methods are unlikely to be effective feature selectors when only a small subset of features matters and the input dimension is much larger than the training set. In that paper’s experiments, L1-regularized logistic regression showed a markedly higher tolerance to many irrelevant features. L1 can also help with very high-dimensional inputs, such as bag-of-words text features.

Sparse weights are not automatically the best starting point for pruning a network, though. Han et al. (2015) found that L1 left more parameters near zero and gave better accuracy straight after pruning, but the remaining connections retrained worse than with L2, and overall L2 gave the best pruning results. If your goal is a smaller deployed network rather than an interpretable feature set, that result argues for treating L1 as one option to test, not the obvious choice.

Lasso, Ridge and Elastic Net: The Regression Names for the Same Penalties

Lasso is linear least-squares regression with L1 regularization, and ridge regression is the L2 version. Lasso is short for “least absolute shrinkage and selection operator”.

Elastic Net combines the two. Scikit-learn’s ElasticNet minimises (1/2n)‖Xw − y‖²₂ + αρ‖w‖₁ + α(1 − ρ)/2 ‖w‖²₂, where ρ is the l1_ratio parameter. Setting l1_ratio=1 gives the Lasso, which scikit-learn notes optimises the same objective as Elastic Net with no L2 penalty. In neural network notation the same combination is λ₁|w| + λ₂w² per weight.

Correlated features are one reason to mix them. Scikit-learn’s guide says that when several features are correlated, Lasso is likely to pick one at random while Elastic Net is likely to pick both. Zou and Hastie (2005) explain this as a grouping effect: the strictly convex Elastic Net penalty tends to give highly correlated variables similar coefficients, whereas the lasso does not even have a unique solution when predictors are identical. In their simulations Elastic Net was more accurate than the lasso in all four examples, cutting prediction error by 18%, 18%, 13% and 27% respectively.

L1 Regularization vs L2: When to Use Each

L2 is the default starting point; L1 earns its place when you actually want inputs removed. CS231n’s notes put it plainly: if you are not concerned with explicit feature selection, L2 can be expected to perform better than L1. Tibshirani’s simulations give a matching picture: lasso did best with a small-to-moderate number of moderate-sized effects, and ridge did best by a good margin with a large number of small effects.

Situation Penalty to start with Why
Most inputs plausibly contribute something L2 (Ridge, weight decay) Prefers diffuse weights that use all inputs a little
Many inputs suspected irrelevant; sparse or interpretable model wanted L1 (Lasso) Drives irrelevant weights to exactly zero
Far more features than samples, few relevant L1 Higher tolerance to many irrelevant features in Ng (2004)
Sparsity wanted, but features are correlated Elastic Net Tends to keep correlated features together rather than picking one
Large number of small effects L2 Ridge did best in Tibshirani’s simulations
Pruning a network for deployment L2 Han et al. found L2 gave the best overall pruning results

Whatever the row, the strength still has to be tuned on validation data.

Why Deep Learning Uses L2 Far More than L1

L2 is more common than L1 in deep learning. The TensorFlow tutorial gives one reason: because the L2 penalty fades to zero for small weights, it regularizes without forcing sparsity, which is one reason it is more common. CS231n adds that L2 tends to perform better whenever explicit feature selection is not the goal.

L1 is also less convenient to implement. Its gradient is λ·sign(w), which is undefined at zero, so L1 is usually not built into optimisers the way weight decay is; it is added to the loss by hand before the backward pass. The tooling reflects this outside deep learning too: several scikit-learn solvers, including lbfgs, newton-cg, newton-cholesky and sag, support only L2 or no regularization.

The third reason is that L2 under plain stochastic gradient descent is the same as weight decay. The TensorFlow tutorial says weight decay is mathematically identical to L2 regularization in the context of neural networks, and Loshchilov and Hutter (2019) confirm the equivalence for standard SGD once rescaled by the learning rate. That equivalence has a boundary, and it matters.

L2 Is Not Weight Decay Under Adam

Loshchilov and Hutter (2019) showed that L2 regularization and weight decay are not equivalent for adaptive gradient methods such as Adam. The reason is visible in the update rule: with L2, the penalty gradient passes through Adam’s per-parameter preconditioner, so the two updates can only match if that preconditioner is a multiple of the identity, which it is not, and therefore no L2 coefficient reproduces weight decay. In their experiments, Adam’s best results with non-zero L2 were comparable to its best results with no L2 at all.

Their fix decouples weight decay from the gradient-based update, giving AdamW. With that change Adam generalised substantially better than with L2, reaching a 15% relative improvement in test error across CIFAR-10 and ImageNet32x32, several training budgets and several learning rate schedules. TensorFlow describes the same split: one approach adds the penalty to the loss, the other runs the optimiser on the raw loss and applies weight decay during the step, as in Ftrl and AdamW.

The practical upshot is an MLOps configuration check, not a theory question. If a model trains with an adaptive optimiser, find out whether the “weight decay” setting you are using adds an L2 term to the gradient or decays the weights directly, because under Adam no L2 coefficient reproduces weight decay.