Lesson 9.1 · 9 min read · about 1 h 10 min to master at a very slow pace

L2 and L1 Penalties

Add a penalty on the size of the weights to the loss. L2 gives weight decay: every step shrinks the weights a little. L1 pushes small weights to exactly zero. The derivation and the difference.

Not startedJump to quiz

What regularisation is

Regularisation is any change to learning meant to reduce the generalisation error rather than the training error. It's the answer to overfitting when you can't get more data. The main methods: penalty terms, early stopping, dropout and data augmentation.

Penalised objective

is the original problem; sends every weight to zero. Biases are not penalised.

L2 is weight decay

, so a gradient step becomes

Every step first shrinks the weights by the factor , then takes the usual step. With and , that's ×0.999 per step. Weights the data don't need decay towards zero; weights the data support are kept. Typical : to on a log grid. In PyTorch, the optimiser's weight_decay argument does this.

L1 makes weights exactly zero

-2-1012-2-1012weight wpenalty pull
L2: λw (fades near 0)L1: λ·sign(w) (constant)
The gradient of each penalty (λ = 1). L2's pull shrinks with the weight, so small weights shrink but never reach 0. L1 pulls with the same force however small the weight, so it drives small weights all the way to 0.

: a constant-size pull towards zero. Small weights end up exactly zero, so connections are removed and unhelpful inputs ignored: sparsity, a form of feature selection.

L1L2
PenaltySum of absolute weightsSum of squared weights
EffectExact zeros (sparse)Many small weights, none exactly zero
UseWhen you want sparsityThe usual choice in deep learning

Small weights give a function that changes slowly with the input, like the degree-4 curve rather than the degree-15 one in lesson 8.1.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passWhy aren't the biases penalised?

A bias only shifts a unit's threshold; it doesn't make the function more wiggly or sensitive to the inputs, so it doesn't drive overfitting. Penalising it would just stop units from positioning their thresholds where the data needs them.

Going deeperIsn't a smaller network a simpler way to regularise?

It is a form of regularisation, but in practice a large network with a penalty usually works better: is a single continuous knob that's easy to tune on validation data, while architecture choices are discrete and expensive to search.

Going deeperIs the penalised objective the same as regularised ERM?

Yes. Empirical risk minimisation (ERM) chooses the parameters that minimise the average training loss, . Regularised ERM minimises instead: exactly the penalised objective above.

Ordinary ERM trusts the training data completely, so a flexible network can fit its noise. The penalty gives up a little training error in exchange for smaller weights, which generalise better. sets the trade-off and is chosen on the validation set. ERM is introduced in Empirical Risk and Population Risk in the machine learning course.

First passWhy do we need both λ and Ω?

They answer different questions. measures how big the weights are; says how much that matters. Think of as counting the crimes and as how seriously they're punished. The same with is ignored, and with a huge dominates the loss. You choose on the validation set; too large and the model underfits.

First passWhy add the penalty to the loss at all?

Training only minimises what's in the loss. On its own, the loss is happy to use huge weights if they fit the training data a little better, noise included. Adding makes "keep the weights small" part of the goal, so the gradient gets an extra pull towards zero, and the network only keeps a large weight when the data really pay for it.

First passWhere does regularisation go in backpropagation? Does it change the forward pass?

No. The forward pass, the error signals and the bias gradients are unchanged. Only the weight gradients get one extra term before the update:

Weight gradientUpdate
None
L1
L2

Worked example

, , , .

  • None:
  • L1: gradient , so
  • L2: gradient , so

Both penalties hold the weight back a little from where the data alone would push it.

First passIn L1, how do I know whether to add +λ or −λ?

Follow the sign of the weight: is for a positive weight and for a negative one. A positive weight gets in its gradient, and since the update subtracts the gradient, the weight goes down. A negative weight gets and goes up. Either way, L1 always pulls towards zero.

First passWhy does L1 reach zero but L2 doesn't?

L1 takes a fixed amount off each step; L2 takes a percentage. Start at with a pull of 0.1 per step (ignoring the data):

  • L1: . It gets there.
  • L2 (×0.9 each step): It keeps shrinking but never reaches 0.

Like eating a cake: a fixed slice each day finishes it; 10% of what's left each day never does. For large weights (), L2's pull is the stronger one; it's near zero that L1 wins.

First passWhen do I use L1 and when L2? Can I use both?
L1L2
PushFixedPercentage
EffectExact zeros (sparsity)Small weights, none zero
Use whenYou suspect many inputs are useless and want them switched off (feature selection)The default in deep learning

Yes, you can use both together; that's called elastic net.

Thumb rule: "L1 → zeros. L2 → small."

Cheatsheet

L2 weight decay

→ . Shrinks weights, none exactly 0. Biases not penalised.

L1 vs L2

L1: pull , exact zeros, sparsity. L2: pull , small weights, the DL default.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateWith L2 weight decay, and , by what factor is each weight shrunk before the usual gradient step?
Q2conceptWhich penalty tends to set many weights to exactly zero?
Q3conceptWhy does L2 shrink small weights but almost never make them exactly zero?
Q4applyYou have 500 input features and suspect only a handful matter. Which penalty fits?
Q5conceptIn the penalised objective , what happens as ?