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 have a single tangent slope everywhere, shrinking smoothly to zero as . The L1 penalty has slope for every , slope for every , and a sharp corner at where no ordinary derivative exists.
To optimize , convex analysis uses a subgradient: the slope of any line through the point that stays on or below the graph of everywhere. Away from zero, the subgradient of is ( for , for ). At , every slope in qualifies; standard code picks , giving
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: . Next differentiate each piece at . The quadratic fit term has slope (pulling upward toward ), while has slope (pulling downward toward with unit force regardless of how large 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: , dropping from 2.125 toward the minimum . Had we used the Ridge penalty slope instead of , we would get and step down to 1.7.
The live model
The left panel plots a 16-step subgradient loss curve starting from ; the right panel compares and as bars. Drag lam up to 3.0 (so 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 because flips abruptly between and . Sixteen steps at that setting cross zero on steps 4, 5, 9, 10, 14, and 15 without ever settling.

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 and one descent step 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].](../assets/ml/the-l1-subgradient--exercise.png)
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 (whenever ), does not shrink to zero as approaches : at with , , and at , . That non-vanishing jump across zero motivates the closed-form soft-threshold proximal step in the next lesson.
Recap
- The subgradient of is , choosing inside .
- Unlike Ridge's , the L1 penalty push has constant magnitude at every nonzero weight.
- Fixed-step subgradient updates descend cleanly on smooth flanks but oscillate across the kink when the optimum sits at zero.