3.2.12 · D4Training Deep Networks

Exercises — L1 - L2 weight decay in deep nets

2,003 words9 min readBack to topic

Quick symbol reminder (all earned in the parent):

  • — a single weight (a number the network learns).
  • learning rate (step size, a small positive number).
  • regularization strength (, how hard we punish big weights).
  • — the data-loss gradient: how much the raw loss wants to change .
  • L2 update: .
  • L1 soft-threshold: .

Level 1 — Recognition

L1-Q1

Which penalty term below is L2, and which is L1?

Recall Solution

is L1 (sum of absolute values → the "lasso"/diamond). is L2 (sum of squares, times → "weight decay"/ridge, circle). The is a convenience so its derivative comes out clean.

L1-Q2

In the update , name the factor and state whether it is above or below for typical small .

Recall Solution

It is the shrink factor. For typical small positive and , the product is a small positive number, so is just below 1. Multiplying by a number below 1 pulls toward 0 every step — that is the "decay."


Level 2 — Application

L2-Q1

, , data gradient , start . Compute after three update steps.

Recall Solution

Shrink factor . With zero data gradient each step is just :

  • Step 1:
  • Step 2:
  • Step 3:

Answer: . This is geometric decay — the weight leaks toward 0 with no data holding it up.

L2-Q2

Same setup but with a constant nonzero data gradient (it pushes up). Find the equilibrium weight where the update leaves unchanged.

Recall Solution

Equilibrium means , so Why? At the upward data push exactly cancels the downward decay . Answer: .

L2-Q3

L1 soft-threshold. , , , start . Apply one soft-threshold step .

Recall Solution

. Then . So . Answer: exactly. The constant toll is bigger than the tiny weight , so it is snapped to zero and stays there — sparsity.


Level 3 — Analysis

L3-Q1

Two weights start at the same value , zero data gradient, . Weight A uses L2 with ; weight B uses L1 with . Which reaches (or approaches) 0 faster, and does either reach exactly 0? Compute the first three values of each.

Recall Solution

L2 (A): shrink factor , so : . Multiplicative — it never hits exactly 0.

L1 (B): subtract each step (soft-threshold), so : . Linear — it will hit exactly 0 at step 10.

Early on they look similar, but see the figure: L2's curve flattens and hugs the axis forever, while L1 marches down a straight line and lands on 0. L1 reaches exact 0; L2 never does.

Figure — L1 - L2 weight decay in deep nets

L3-Q2

Explain, using the equilibrium formula , why doubling halves every surviving weight but does not create sparsity in L2.

Recall Solution

is inversely proportional to . Doubling makes half as large — every weight shrinks proportionally. But a weight only reaches 0 if its data gradient is 0; otherwise is some nonzero fraction, never exactly 0. So L2 gives uniformly smaller weights, not zeros. Sparsity needs the constant-force L1 push that can overpower a small weight regardless of -proportionality.


Level 4 — Synthesis

L4-Q1

A weight has data gradient (constant), . Under L2 it settles at . Under L1 with the same , does this weight survive or die? Use the survival condition: an L1 weight is killed iff the constant penalty force exceeds the data force .

Recall Solution

Survival test: compare against . Since , the constant L1 toll beats the data's pull — the weight is driven to 0 (dies).

Contrast: under L2 the same weight settles at (survives, just small). This is the punchline: with identical , L2 keeps the weight (shrunk), L1 kills it. L1 does automatic feature selection — a feature whose gradient can't out-muscle the toll gets removed entirely. See Lasso and Ridge Regression.

Figure — L1 - L2 weight decay in deep nets

L4-Q2

For plain SGD, L2 penalty and "weight decay" are identical (as derived in the parent). For Adam, they are not. Given Adam's per-parameter effective step , explain why adding L2 to the gradient does not give clean decay, and what AdamW does instead.

Recall Solution

In Adam the gradient (including the L2 term ) is divided by each parameter's own . So the effective decay on weight is not the clean constant . Parameters with large gradient history get their decay shrunk; parameters with small get it amplified. The intended uniform shrink is scrambled.

AdamW fixes this by decoupling: it applies the adaptive step to the data gradient only, then adds the decay as a separate, un-scaled term: Now every weight decays by the same factor regardless of . That's why AdamW ≠ Adam + L2. See Adam and AdamW Optimizers.


Level 5 — Mastery

L5-Q1

Design a soft-threshold function (the L1 update operator with threshold ) and evaluate it at with . Then state in words what does geometrically.

Recall Solution

With :

  • :
  • :
  • :
  • :

Geometrically: pulls every value toward zero by a fixed amount , and anything already within of zero is flattened onto 0 — creating a "dead zone" that maps to exactly 0. That dead zone is the source of sparsity; L2's shrink has no dead zone, only a slope through the origin.

Figure — L1 - L2 weight decay in deep nets

L5-Q2

Half-life of a weight under pure L2 decay. With zero data gradient, . Given , , find how many steps until a weight drops to half its starting value. (Use .)

Recall Solution

We need . Here . Take of both sides (the tool that turns the exponent into a multiplier we can solve for): Answer: about steps to halve. Larger ⇒ faster decay ⇒ shorter half-life. This is exactly radioactive-decay maths applied to weights — connect to Gradient Descent.

L5-Q3

Synthesis with priors. Argue, from the Maximum a Posteriori (MAP) Estimation view, why L1's Laplace prior produces exact zeros while L2's Gaussian prior does not. One sentence on the shape of each prior at .

Recall Solution

The MAP objective adds to the loss. The Laplace prior has a sharp corner (kink) at — its negative-log is , whose slope jumps from to across 0, so 0 is a strong attractor the optimizer can rest exactly on. The Gaussian prior is smooth and flat at (zero slope there), so there's no kink pulling the weight onto 0 — it only gently squeezes. Sharp corner ⇒ exact zeros (L1); smooth bump ⇒ small-but-nonzero (L2). See Overfitting and Generalization for why sparsity aids generalization.


Recall One-line takeaways for the whole set

L2 = multiply-shrink, never zero, half-life set by · L1 = subtract-fixed-toll, exact zeros via soft-threshold dead zone · equilibrium · Adam breaks clean decay, AdamW restores it · corner-prior (Laplace) ⇒ sparse, smooth-prior (Gaussian) ⇒ dense.