3.2.12Training Deep Networks

L1 - L2 weight decay in deep nets

1,970 words9 min readdifficulty · medium

WHY do we regularize weights at all?

A deep net has millions of parameters. With that much freedom it can memorize noise in the training set (huge, spiky weights that carve out weird bumps to hit every training point). That kills generalization.

The cure: add a penalty on weight size to the loss. Now the optimizer must trade off:

  • lowering the data loss (fit the points), vs.
  • keeping w\|w\| small (stay simple).

This is exactly the bias–variance tradeoff: we accept a little more bias to cut variance.


WHAT are L1 and L2 penalties?


HOW L2 becomes literal "weight decay" — derive it

Start from plain gradient descent on the regularized loss with L2:

J~(w)=J(w)+λ2w22\tilde J(w) = J(w) + \frac{\lambda}{2}\|w\|_2^2

Take the gradient: wJ~=wJ+λw\nabla_w \tilde J = \nabla_w J + \lambda w Why this step? wi(λ2wi2)=λwi\frac{\partial}{\partial w_i}\left(\frac{\lambda}{2}w_i^2\right)=\lambda w_i, so the whole penalty gradient is just λw\lambda w.

Now the update rule with learning rate η\eta: wwη(wJ+λw)w \leftarrow w - \eta(\nabla_w J + \lambda w)

Rearrange: w(1ηλ)shrink factorw    ηwJw \leftarrow \underbrace{(1-\eta\lambda)}_{\text{shrink factor}}\,w \;-\; \eta\,\nabla_w J

Why this is beautiful: even before any data pushes the weights, they leak toward zero each step. A weight survives only if the data gradient keeps re-supplying it.


HOW L1 produces sparsity — the geometry

Look at the L1 gradient: w(λw1)=λsign(w)\nabla_w \left(\lambda\|w\|_1\right) = \lambda\,\text{sign}(w) Why this step? ddww=sign(w)\frac{d}{dw}|w| = \text{sign}(w) (undefined at 0, handled by subgradient).

The update becomes: wwηwJηλsign(w)w \leftarrow w - \eta\,\nabla_w J - \eta\lambda\,\text{sign}(w)

Notice the penalty term ηλsign(w)\eta\lambda\,\text{sign}(w) is a constant push toward zero of fixed size, regardless of how big ww is. For L2, the push ηλw\eta\lambda w shrinks as w0w\to 0, so it never quite reaches zero. For L1 the push stays full-strength until it crosses zero — so many weights get snapped exactly to 0.

The classic picture: minimizing data loss (elliptical contours) subject to a budget on weights. The L1 budget is a diamond (corners on the axes), the L2 budget is a circle. The loss contour first touches the diamond at a corner (a weight = 0), but touches the circle anywhere.

Figure — L1 - L2 weight decay in deep nets

Bayesian WHY (steel-manning the choice)

Adding a penalty = putting a prior on the weights and doing MAP estimation:

  • L2 ⇔ Gaussian prior wN(0,1/λ)w \sim \mathcal N(0, 1/\lambda) (log-prior λw2\propto -\lambda w^2).
  • L1 ⇔ Laplace prior (peaked at 0, heavy tails) → encourages exact zeros.

So regularization is not a hack — it's a statement of belief: "most weights should be near zero."


Worked examples


Common mistakes


The 80/20 core


Recall Feynman: explain to a 12-year-old

Imagine you're packing a backpack for a hike. Every item is a "weight" the network wants to carry. Weight decay is a rule: "You can bring anything, but each item costs you energy, so only pack it if it truly helps." L2 says every item slowly gets lighter each step, so useless items fade away but nothing is fully banned. L1 says every item costs a fixed toll, so the almost-useless ones get thrown out completely — you end up with a small, tidy backpack. A tidy backpack (simple network) is easier to carry on a new trail (new data).


Flashcards

What penalty term does L2 add to the loss?
λ2w22\frac{\lambda}{2}\|w\|_2^2
Derive the L2 gradient-descent update in "decay" form.
w(1ηλ)wηwJw \leftarrow (1-\eta\lambda)w - \eta\nabla_w J; the (1ηλ)(1-\eta\lambda) factor shrinks weights each step.
Why is L2 literally called "weight decay"?
Because each update first multiplies ww by a factor <1<1, so weights decay toward zero unless data re-supplies them.
Which of L1/L2 produces exact zeros and why?
L1; its gradient is λsign(w)\lambda\,\text{sign}(w) — a constant-magnitude push that drives small weights all the way to 0.
Why doesn't L2 produce exact zeros?
Its push λw\lambda w shrinks proportionally to ww, vanishing as w0w\to0, so weights get tiny but not exactly zero.
Equilibrium weight under L2 (zero data gradient case)?
w=1λwJw^\star = -\frac{1}{\lambda}\nabla_w J; larger λ\lambda ⇒ smaller weight.
What prior corresponds to L2? To L1?
L2 ⇔ Gaussian prior; L1 ⇔ Laplace prior.
Why not regularize bias terms?
They only shift the function; penalizing them adds bias without reducing variance and can worsen the fit.
What goes wrong if λ\lambda is too large?
All weights shrink toward 0 ⇒ underfitting (high bias).
Why is AdamW different from Adam + L2?
AdamW decouples weight decay from the adaptive gradient scaling, so decay isn't distorted by per-parameter learning rates.
Soft-threshold operator for L1?
wsign(w)max(wηλ,0)w \leftarrow \text{sign}(w)\max(|w|-\eta\lambda,\,0).

Connections

Concept Map

cured by

added to loss

controls tradeoff

strength

option A

option B

gradient lambda w

shrink factor 1 minus eta lambda

gradient lambda sign w

snaps weights

smoother function

fewer active weights

Overfitting: memorize noise

Penalty on weight size

Regularized objective J tilde

Bias-variance tradeoff

Lambda hyperparameter

L2 penalty: half sum w squared

L1 penalty: sum abs w

Weight decay update

Small nonzero weights

Constant push to zero

Sparse network / feature selection

Better generalization

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, deep network mein millions of weights hote hain, aur itni freedom ke saath network training data ko ratta maar leta hai — noise tak fit kar deta hai, spiky weird function ban jaata hai. Yeh overfitting hai. Iska solution: loss ke saath weights ke size ka ek penalty jod do. Ab optimizer ko do cheezein balance karni padti hain — data fit karo, par weights ko chhota rakho. Isko regularization ya weight decay kehte hain, aur strength ko λ\lambda control karta hai.

L2 (λ2w2\frac{\lambda}{2}\|w\|^2) ka magic yeh hai: update likho to ban jaata hai w(1ηλ)wηJw \leftarrow (1-\eta\lambda)w - \eta\nabla J. Yaani har step mein weight pehle ek factor (1ηλ)(1-\eta\lambda) se chhota ho jaata hai — isliye naam "weight decay"! Weight tabhi zinda rehta hai jab data usko baar-baar wapas push kare. Iska equilibrium point hai w=J/λw^\star = -\nabla J/\lambda — matlab data ko har weight "kamaana" padta hai.

L1 (λw1\lambda\|w\|_1) alag kaam karta hai. Iska gradient hai λsign(w)\lambda\,\text{sign}(w) — ek constant force jo weight ke size par depend nahi karti. Isliye chhote weights ko yeh seedha zero par patak deti hai. Result: sparse network (bahut saare weights exactly 0). L2 sab weights ko chhota karta hai par zero nahi; L1 kuch ko poori tarah kaat deta hai — feature selection jaisa.

Ek galti se bacho: λ\lambda bahut bada mat karo, warna sab weights zero ki taraf bhaag jaayenge aur underfitting ho jaayega. Aur yaad rakho — Adam ke saath simple L2 same nahi hota decay ke; isliye AdamW use karte hain jo decay ko alag rakhta hai. Validation set par λ\lambda tune karo, bas.

Go deeper — visual, from zero

Test yourself — Training Deep Networks

Connections