Coordinate Descent
Coordinate descent minimizes over one variable (or one block of variables) at a time, holding the others fixed. Each step is a one-dimensional problem, often with a closed-form solution. That simplicity makes it the fastest method for some important problems, most famously the lasso and elastic net in glmnet and scikit-learn.
The method
Cycle through the coordinates :
For a quadratic , setting gives : exactly the Gauss-Seidel iteration for .
import numpy as np
A = np.array([[4.0, 1.0, 0.5], [1.0, 3.0, 0.2], [0.5, 0.2, 2.0]]); b = np.array([1.0, 2.0, 3.0])
x = np.zeros(3)
f = lambda x: 0.5 * x @ A @ x - b @ x
for sweep in range(1, 9):
for i in range(3):
x[i] = (b[i] - A[i] @ x + A[i, i] * x[i]) / A[i, i]
print(f"sweep {sweep}: f = {f(x):.10f}")
print("exact minimum f =", round(float(f(np.linalg.solve(A, b))), 10))sweep 1: f = -2.5375173611
sweep 2: f = -2.7464630339
sweep 3: f = -2.7468293494
sweep 4: f = -2.7468294954
sweep 5: f = -2.7468294974
sweep 6: f = -2.7468294974
sweep 7: f = -2.7468294974
sweep 8: f = -2.7468294974
exact minimum f = -2.7468294974
Coordinate descent for the lasso
The lasso, , is not differentiable, but its one-dimensional problems are easy. With the partial residual , the update is a soft-thresholding:
The threshold sets small coefficients exactly to zero, which is how the lasso selects features.
import numpy as np
rng = np.random.default_rng(0)
n, d = 100, 8
X = rng.normal(size=(n, d))
w_true = np.array([3.0, -2.0, 0, 0, 1.5, 0, 0, 0])
y = X @ w_true + 0.5 * rng.normal(size=n)
soft = lambda z, t: np.sign(z) * max(abs(z) - t, 0.0)
def lasso_cd(X, y, lam, sweeps=200):
w = np.zeros(X.shape[1]); r = y.copy()
col_sq = (X**2).sum(axis=0)
for _ in range(sweeps):
for j in range(X.shape[1]):
r += X[:, j] * w[j] # remove coordinate j
w[j] = soft(X[:, j] @ r, lam) / col_sq[j]
r -= X[:, j] * w[j] # add it back
return w
w = lasso_cd(X, y, lam=20.0)
print("lasso weights:", np.round(w, 3) + 0)
from sklearn.linear_model import Lasso
sk = Lasso(alpha=20.0 / n, fit_intercept=False).fit(X, y) # sklearn divides the loss by n
print("scikit-learn: ", np.round(sk.coef_, 3) + 0)lasso weights: [ 2.691 -1.754 0. 0. 1.311 0. 0. 0. ]
scikit-learn: [ 2.691 -1.754 0. 0. 1.311 0. 0. 0. ]
Five coefficients are exactly zero, and the result matches scikit-learn, which uses the same algorithm.
When coordinate descent works
| Condition | Effect |
|---|---|
| smooth convex | converges to the minimum |
| = smooth convex + separable nonsmooth (, e.g. L1) | converges (the lasso case) |
| nonsmooth term couples variables (e.g. ) | can get stuck at non-optimal points |
| cheap 1D updates (residual bookkeeping) | each sweep costs about one gradient evaluation |
Variables that are strongly correlated slow it down, as with Gauss-Seidel on ill-conditioned matrices. Randomized coordinate order often gives better theoretical guarantees.
A failure case
import numpy as np
# f(x, y) = |x - y| + 0.1 (x + y)^2 ... minimum at (0, 0), but from (1, 1) no single coordinate helps
f = lambda x, y: abs(x - y) + 0.1 * (x + y)**2
x = y = 1.0
grid = np.linspace(-3, 3, 60001)
for sweep in range(3):
x = grid[np.argmin([f(z, y) for z in grid])]
y = grid[np.argmin([f(x, z) for z in grid])]
print("coordinate descent stops at", (round(float(x), 3), round(float(y), 3)), " f =", round(f(x, y), 4), " true minimum f(0, 0) = 0")coordinate descent stops at (1.0, 1.0) f = 0.4 true minimum f(0, 0) = 0
At , moving alone or alone increases faster than it decreases the smooth part, yet moving both together reaches 0.
In software
- scikit-learn's
Lasso,ElasticNetandLassoCV, and R'sglmnet, use cyclic coordinate descent with "warm starts" along a path of values. LinearSVC(liblinear) solves the SVM dual by coordinate descent; matrix factorization for recommender systems uses alternating least squares, a block-coordinate method.
Exercises
Exercise 1: One coordinate update
For , starting at , do one sweep ( then ).
Answer
, then .
Solution
with gives . Then gives .
Exercise 2: Soft threshold
Compute , and .
Answer
3, 0 and .
Solution
Shrink the magnitude by 2 and clip at zero, keeping the sign.
Exercise 3: Bigger (code)
Rerun lasso_cd with . How many weights are non-zero?
Answer
Only one: the weight of the strongest feature, .
Solution
import numpy as np
rng = np.random.default_rng(0)
X = rng.normal(size=(100, 8)); y = X @ np.array([3.0, -2.0, 0, 0, 1.5, 0, 0, 0]) + 0.5 * rng.normal(size=100)
soft = lambda z, t: np.sign(z) * max(abs(z) - t, 0.0)
w = np.zeros(8); r = y.copy(); cs = (X**2).sum(axis=0)
for _ in range(200):
for j in range(8):
r += X[:, j] * w[j]; w[j] = soft(X[:, j] @ r, 200.0) / cs[j]; r -= X[:, j] * w[j]
print(np.round(w, 3) + 0, " non-zero:", int(np.sum(w != 0)))[0.412 0. 0. 0. 0. 0. 0. 0. ] non-zero: 1
Test yourself
Coordinate descent on a convex quadratic is the same as…
- Newton's method
- the Gauss-Seidel iteration
- the simplex method
- gradient descent with step 1
Why does coordinate descent work for the lasso even though is not differentiable?
- The L1 term is separable across coordinates
- The lasso is not convex
- It uses the Hessian
- It ignores the L1 term
The soft-threshold with returns…