Speed

The L-one Subgradient

Compute the subgradient of the 1D Lasso objective, take one subgradient descent step, and watch the trajectory approach or bounce across the zero kink.

The problem

Smooth penalties such as 12λw2\frac{1}{2}\lambda w^2 have a single tangent slope λw\lambda w everywhere, shrinking smoothly to zero as w→0w \to 0. The L1 penalty λ∣w∣\lambda |w| has slope +λ+\lambda for every w>0w > 0, slope −λ-\lambda for every w<0w < 0, and a sharp corner at w=0w = 0 where no ordinary derivative exists.

To optimize J(w)=12(w−z)2+λ∣w∣J(w) = \frac{1}{2}(w - z)^2 + \lambda |w|, convex analysis uses a subgradient: the slope of any line through the point that stays on or below the graph of ∣w∣|w| everywhere. Away from zero, the subgradient of ∣w∣|w| is sign⁡(w)\operatorname{sign}(w) (+1+1 for w>0w > 0, −1-1 for w<0w < 0). At w=0w = 0, every slope in [−1,+1][-1, +1] qualifies; standard code picks sign⁡(0)=0\operatorname{sign}(0) = 0, giving

g=(w−z)+λsign⁡(w),wnext=w−η g g = (w - z) + \lambda \operatorname{sign}(w), \qquad w_{\text{next}} = w - \eta\, g

Work through the numbers

Take starting weight w = 2.0, unconstrained data target z = 2.5, penalty lam = 1.0, and step size lr = 0.2 (input = [2.0, 2.5]).

First compute the forward objective: J(2.0)=12(2.0−2.5)2+1.0 ∣2.0∣=0.125+2.0=2.125J(2.0) = \frac{1}{2}(2.0 - 2.5)^2 + 1.0\,|2.0| = 0.125 + 2.0 = 2.125. Next differentiate each piece at w=2.0w = 2.0. The quadratic fit term has slope w−z=2.0−2.5=−0.5w - z = 2.0 - 2.5 = -0.5 (pulling ww upward toward 2.52.5), while λ∣w∣\lambda |w| has slope 1.0⋅sign⁡(2.0)=+1.01.0 \cdot \operatorname{sign}(2.0) = +1.0 (pulling ww downward toward 00 with unit force regardless of how large ww is). Adding both gives subgradient g = -0.5 + 1.0 = 0.5.

One descent step moves to w_next = 2.0 - 0.2 * 0.5 = 1.9, returning [g, w_next] = [0.5, 1.9]. Checking the new objective confirms progress: J(1.9)=12(−0.6)2+1.9=2.08J(1.9) = \frac{1}{2}(-0.6)^2 + 1.9 = 2.08, dropping from 2.125 toward the minimum J(1.5)=2.0J(1.5) = 2.0. Had we used the Ridge penalty slope λw=2.0\lambda w = 2.0 instead of λsign⁡(w)\lambda \operatorname{sign}(w), we would get g=−0.5+2.0=1.5g = -0.5 + 2.0 = 1.5 and step down to 1.7.

The live model

The left panel plots a 16-step subgradient loss curve J(wt)J(w_t) starting from w0=2.0w_0 = 2.0; the right panel compares ∣w0∣|w_0| and ∣w1∣|w_1| as bars. Drag lam up to 3.0 (so ∣z∣<λ|z| < \lambda and the true minimizer is 0.0) and increase lr to 0.35: instead of settling smoothly at zero, fixed-step subgradient descent chatters back and forth across w=0w = 0 because sign⁡(w)\operatorname{sign}(w) flips abruptly between +1+1 and −1-1. Sixteen steps at that setting cross zero on steps 4, 5, 9, 10, 14, and 15 without ever settling.

16-step L1 subgradient objective curve starting from w = 2.0 with z = 2.5, lam = 1.0, lr = 0.2, showing g = 0.5 and first step w_next = 1.9.
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let lr = scene.parameter("lr", 0.2, 0.05, 0.45, 0.05);
let w = input.select(0, 0);
let z = input.select(0, 1);
let g = (w - z) + w.sign() * lam;
let w_next = w - g * lr;
let pair = torch.list().add(g.reshape(1)).add(w_next.reshape(1));
let answer = torch.cat(pair, 0);
out.set("subgrad_step", answer);
{
  "subgrad_step": [
    0.5,
    1.9
  ]
}

Your turn

Compute the L1 subgradient gg and one descent step wnextw_{\text{next}} for input = [w, z] at lam and lr, returning [g, w_next]. On the default input [2.0, 2.5] the starter returns [1.5, 1.7] where the page computed [0.5, 1.9].

Starter uses smooth L2 slope lam * w instead of the L1 subgradient, producing [1.5, 1.7] instead of [0.5, 1.9].
let lam = scene.parameter("lam", 1.0, 0.0, 3.5, 0.5);
let lr = scene.parameter("lr", 0.2, 0.05, 0.45, 0.05);
let w = input.select(0, 0);
let z = input.select(0, 1);
let g = (w - z) + w * lam;
let w_next = w - g * lr;
let pair = torch.list().add(g.reshape(1)).add(w_next.reshape(1));
let answer = torch.cat(pair, 0);
out.set("subgrad_step", answer);
{
  "subgrad_step": [
    1.5,
    1.7
  ]
}

Return [g, w_next] for one subgradient descent step on the 1D Lasso objective from input = [w, z] at lam and lr. On the default input the starter returns [1.5, 1.7] where the page computed [0.5, 1.9].

Read the result carefully

Notice why subgradient descent behaves differently from smooth gradient descent near sparsity. When the true solution is w∗=0w^* = 0 (whenever ∣z∣≤λ|z| \le \lambda), gg does not shrink to zero as ww approaches 00: at w=+0.01w = +0.01 with z=0.5,λ=1.0z = 0.5, \lambda = 1.0, g=0.01−0.5+1.0=+0.51g = 0.01 - 0.5 + 1.0 = +0.51, and at w=−0.01w = -0.01, g=−0.01−0.5−1.0=−1.51g = -0.01 - 0.5 - 1.0 = -1.51. That non-vanishing jump across zero motivates the closed-form soft-threshold proximal step in the next lesson.

Recap

  • The subgradient of 12(w−z)2+λ∣w∣\frac{1}{2}(w - z)^2 + \lambda |w| is g=(w−z)+λsign⁡(w)g = (w - z) + \lambda \operatorname{sign}(w), choosing sign⁡(0)=0\operatorname{sign}(0) = 0 inside [−1,+1][-1, +1].
  • Unlike Ridge's λw\lambda w, the L1 penalty push has constant magnitude λ\lambda at every nonzero weight.
  • Fixed-step subgradient updates w−ηgw - \eta g descend cleanly on smooth flanks but oscillate across the w=0w = 0 kink when the optimum sits at zero.