5.6.3 · D4Machine Learning (Aerospace Applications)

Exercises — Regularization — L1 (lasso), L2 (ridge), dropout

3,059 words14 min readBack to topic

This page is a graded workout for Regularization — L1, L2, dropout. Every symbol used here was built in the parent note; if you meet one you forgot, jump back. Work each problem before opening its solution — the whole point is to catch your own gaps.

Quick symbol refresher, so line one is followable:

Recall The six symbols you need
  • (or ) ::: a weight — one tunable number the model multiplies a feature by.
  • (eta) ::: the learning rate — how big a step gradient descent takes.
  • (lambda) ::: regularization strength — how hard we punish big weights.
  • ::: the data gradient — the slope of the plain (unpenalized) loss.
  • ::: dropout probability — chance a neuron is switched off during training.
  • ::: the dropout mask — a coin-flip variable, , that is (keep the neuron) with probability and (drop it) with probability .

The four update rules everything below rests on. Read the why beside each — the shape of the penalty is the rule:


Level 1 — Recognition

Exercise 1.1 (L1 · Recognition)

For each phrase, name the technique (L1 / L2 / dropout): (a) "penalizes the squared magnitude of weights" (b) "randomly switches neurons off during training" (c) "drives many weights to exactly zero" (d) "also called weight decay"

Recall Solution

(a) L2 (ridge) — the squared term . (b) dropout — Bernoulli masking of activations. (c) L1 (lasso) — the diamond corners land on zeroed coordinates. (d) L2 (ridge) — the factor shrinks weights every step, i.e. decay.

Exercise 1.2 (L1 · Recognition)

A network is at test time. Under standard inverted dropout, what do you do to the activations?

Recall Solution

Nothing. Test time uses the full network with no mask and no scaling, because the scaling was already applied during training. So .


Level 2 — Application

Exercise 2.1 (L2 · Application)

Given , , a weight , and data gradient . Compute one L2 update step.

Recall Solution

Use .

  • Decay factor: .
  • . The weight shrank both from decay () and from the data gradient ().

Exercise 2.2 (L2 · Application)

Same , , but now turn off the data gradient (). Starting at , how many steps until ? (This shows pure decay is geometric.)

Recall Solution

With no data gradient, each step multiplies by : . We want . Because counts whole steps, a value of means step 27 is not yet enough — we must round up to the next integer: steps. (Always take the ceiling when solving "how many discrete steps until a threshold" — a fractional step doesn't exist.) This is why L2 shrinks but never zeroes a weight — geometric decay approaches zero without reaching it. (Note this holds only because ; if the geometric picture breaks — see Exercise 5.2.)

Exercise 2.3 (L1 · Application)

Given , , so . Apply the soft-threshold rule to each weight (pure penalty step): .

Figure — Regularization — L1 (lasso), L2 (ridge), dropout
Recall Solution

Rule: .

  • : .
  • : , , sign .
  • : .
  • : , sign . Result: . Two weights killed exactly, the survivors shrank by the same constant .

Figure s01 (described): the horizontal axis is the incoming weight , the vertical axis is its new value. A dashed black diagonal is the "do nothing" identity line (). The red curve is the soft-threshold output: it is a flat line stuck at zero across the whole band (shaded red — the "dead zone"), then it splits off parallel to the identity line but shifted toward zero by . Any weight landing inside the red band snaps to exactly zero; anything outside survives but pulled closer to zero.


Level 3 — Analysis

Exercise 3.1 (L3 · Analysis)

Two weights start equal at . One is trained with L2 ( per step), the other with L1 (constant per step, soft-threshold). Ignore the data gradient. After exactly 6 steps, which is smaller, and which (if any) is zero?

Figure — Regularization — L1 (lasso), L2 (ridge), dropout
Recall Solution

L2: — still positive, never exactly zero. L1: subtract each step: after steps we'd subtract , but it hits zero first.

  • . That is subtractions, landing exactly at on step 6. So L1 is smaller (=0); L2 is . This is the whole story: L1 sparsifies, L2 only shrinks.

Figure s02 (described): the horizontal axis is the training step (0 to 6), the vertical axis is the weight value. A black curve (circles) is L2: it bends, each point the last, flattening as it approaches — but never reaching — the zero line. A red line (squares) is L1: a perfectly straight downhill ramp dropping per step, striking the zero axis exactly at , where a red dot marks the landing. The straight red ramp vs the bending black curve is the entire L1-vs-L2 contrast in one picture.

Exercise 3.2 (L3 · Analysis)

Explain, using the constraint-region picture, why L1 zeros coordinates but L2 does not. Reference the diamond vs circle.

Figure — Regularization — L1 (lasso), L2 (ridge), dropout
Recall Solution

Regularization = minimize data loss subject to a budget on weights. The budget region is:

  • L2: — a smooth circle (no special points).
  • L1: — a diamond whose corners sit on the axes (one coordinate ). The data-loss contours are ellipses that expand until they first touch the budget region. A smooth circle is touched at a generic point → both weights nonzero. A diamond is most likely touched at a sharp corner → one weight exactly zero. That corner-catching is geometric sparsity.

Figure s03 (described): centred on the origin are two black budget shapes — a solid circle (L2) and a dashed diamond whose four corners sit exactly on the and axes (L1). Off to the right sit faint black concentric ellipses, the data-loss contours, growing outward from their own centre. Where the growing ellipse first meets the diamond is a red dot sitting right on the -axis, i.e. at — a sparse solution. The corner reaches out and "catches" the contour; a smooth circle would have been grazed at an off-axis point with both weights nonzero.


Level 4 — Synthesis

Exercise 4.1 (L4 · Synthesis)

A hidden layer outputs activation . You use inverted dropout with . (a) During training, when the neuron is kept, what value flows forward? (b) Verify the expected training activation equals the raw . (c) What flows forward at test time?

Recall Solution

Keep probability . Inverted rule: kept value . (a) . (b) . ✓ Expectation is preserved — that is the entire reason to scale up during training. (c) Test time uses no mask, no scaling: .

Exercise 4.2 (L4 · Synthesis)

You must predict aircraft drag from 50 candidate features, but you know only a handful are physically relevant and you want the model to tell you which. You also want smooth, stable coefficients on the survivors. Which regularizer(s) do you pick, and why? What would you tune with cross-validation?

Recall Solution

Use L1 (or Elastic Net = L1 + L2):

  • L1 performs automatic feature selection — zeroing the irrelevant 45-ish sensors, doing feature selection for free and giving interpretable results.
  • Adding a small L2 term keeps the surviving coefficients stable when features are correlated (pure L1 arbitrarily picks one of two correlated sensors). Tune (and the L1/L2 mix) by cross-validation: sweep , pick the value minimizing validation error. Confirm you are not overfitting with overfitting detection (train-vs-validation gap).

Level 5 — Mastery

Exercise 5.1 (L5 · Mastery)

A fault-detection network overfits: train accuracy 99%, test 78%. You add dropout on the fully-connected layers and get train 92%, test 89%. (a) By how many points did the generalization gap (train − test) shrink? (b) Training accuracy fell. Explain why that is expected, not a bug. (c) Name one alternative regularizer and one complementary diagnostic from the connections list.

Recall Solution

(a) Before: gap pts. After: gap pts. Shrinkage points. (b) Each training step now runs a random sub-network, so the model can no longer memorize exact examples — training accuracy must drop. We trade a little training fit for a large test gain: the whole objective of regularization. (c) Alternative regularizer: L2 weight decay on the same fully-connected layers (or ensemble methods — dropout is itself an implicit ensemble of sub-networks, so building a real ensemble pushes generalization the same direction). Complementary diagnostic: overfitting detection — plot the train-vs-validation accuracy curves and watch the gap; pair it with cross-validation to actually choose the dropout rate rather than guessing .

Exercise 5.2 (L5 · Mastery)

Derive, from the plain gradient-descent update, the L2 "weight decay" form, and state precisely the condition on for the decay to be stable (weights don't blow up or oscillate).

Recall Solution

Start from total loss (sum over weights only — the bias is excluded by convention). Its gradient w.r.t. : (the cancels the exponent's — that's why we put it there). Plug into : The factor is weight decay. Stability of the pure-decay part : geometric with ratio .

  • Decays monotonically toward 0 when .
  • Boundary : , so the weight collapses to exactly zero in a single step — usable but drastic (all decay, no memory of the old value).
  • If then : the weight flips sign each step (oscillates while shrinking); if then and it diverges. So keep for clean shrinkage; treat as the knife-edge and anything above it as unsafe.
Recall Self-check summary

L1 vs L2 in one line? ::: L1 subtracts a constant (→ sparsity), L2 multiplies by (→ smooth shrinkage, never zero). What is and why does it matter? ::: Formally undefined (a subgradient anywhere in ); optimizers set it to so a weight already at zero stays put — which is why soft-thresholding is the safe form. Dropout at test time? ::: Nothing — inverted dropout already scaled during training. Why divide by during training? ::: To preserve the expected activation () so no rescaling is needed at test. L2 stability range? ::: for clean decay; collapses a weight to zero in one step; diverges. Do we regularize the bias? ::: No — by convention is excluded from the penalty since it shifts, not scales, and cannot cause overfitting.