Subgradients
Many useful objectives have kinks: the absolute value, the hinge loss, the maximum of several functions, the L1 norm. At a kink there is no gradient, but for convex functions there is a set of "supporting slopes", the subdifferential, which is enough to state optimality and to build algorithms.
Definition
A vector is a subgradient of a convex at if the linear function it defines lies below everywhere:
The set of all subgradients is the subdifferential . If is differentiable at , .
| Function | Subdifferential |
|---|---|
| for , at , for | |
| (hinge) | for , at , for |
| componentwise: , or where | |
| convex hull of over the active | |
| for ; the unit ball at 0 |
Optimality
For convex , is a global minimum if and only if
For : when , so the minimum is exactly at 0 for every in . That is the soft-thresholding effect behind the lasso.
The subgradient method
Replace the gradient with any subgradient: . Two differences from gradient descent:
- A subgradient step can increase , so track the best point seen.
- A fixed step does not converge to the minimum; use diminishing steps such as . The rate is , slow, but it works for any convex Lipschitz function.
import numpy as np
# L1 regression: minimize sum |X w - y| by the subgradient method
rng = np.random.default_rng(0)
X = rng.normal(size=(60, 3)); w_true = np.array([1.0, -2.0, 0.5])
y = X @ w_true + rng.laplace(scale=0.3, size=60)
f = lambda w: np.abs(X @ w - y).sum()
w = np.zeros(3); best = (f(w), w.copy())
for k in range(20000):
g = X.T @ np.sign(X @ w - y) # a subgradient
w = w - 0.05 / np.sqrt(k + 1) * g
if f(w) < best[0]:
best = (f(w), w.copy())
if k + 1 in (10, 100, 1000, 20000):
print(f"k = {k + 1:5d}: best f = {best[0]:.4f}")
print("best w:", np.round(best[1], 3))k = 10: best f = 24.3758
k = 100: best f = 18.0865
k = 1000: best f = 18.0837
k = 20000: best f = 18.0818
best w: [ 0.953 -1.919 0.49 ]
Subgradients of the hinge loss: the SVM
The SVM objective is strongly convex but not smooth. Pegasos runs stochastic subgradient steps with :
import numpy as np
rng = np.random.default_rng(1)
n = 200
X = np.vstack([rng.normal(loc=[2, 2], size=(n // 2, 2)), rng.normal(loc=[-2, -2], size=(n // 2, 2))])
y = np.r_[np.ones(n // 2), -np.ones(n // 2)]
lam = 0.01; w = np.zeros(2)
for k in range(1, 5001):
i = rng.integers(n)
g = lam * w - (y[i] * X[i] if y[i] * (w @ X[i]) < 1 else 0)
w = w - g / (lam * k)
acc = np.mean(np.sign(X @ w) == y)
obj = lam / 2 * w @ w + np.mean(np.maximum(0, 1 - y * (X @ w)))
print("w =", np.round(w, 3), " training accuracy:", acc, " objective:", round(float(obj), 4))w = [0.606 0.656] training accuracy: 0.995 objective: 0.0166
In software
- PyTorch and JAX return a particular subgradient at kinks (for example, the derivative of ReLU at 0 is taken as 0), which is why networks with ReLU and L1 penalties still train.
- Subgradient methods are slow; when the nonsmooth part is simple, proximal methods are much faster.
Exercises
Exercise 1: Subdifferential of a max
Find for .
Answer
.
Solution
At both pieces equal 1, so both are active; the subdifferential is the convex hull of their slopes 1 and 2.
Exercise 2: Optimality
Minimize using .
Answer
, the median.
Solution
For between 1 and 6 but not at 2, the slope is , nonzero. At : .
Exercise 3: A fixed step stalls (code)
Minimize from with the fixed step . What happens?
Answer
The iterates bounce around 0 forever and never get closer than about 0.1.
Solution
x, xs = 1.0, []
for k in range(8):
x -= 0.3 * (1 if x > 0 else -1 if x < 0 else 0)
xs.append(round(x, 2))
print(xs)[0.7, 0.4, 0.1, -0.2, 0.1, -0.2, 0.1, -0.2]
The subgradient keeps magnitude 1 however close you are, so a fixed step overshoots. Diminishing steps fix this.
Test yourself
The subdifferential of at is…
- empty
For convex , is a minimum if and only if…
- exists
- is empty
Why does the subgradient method track the best point seen?
- A subgradient step may increase
- To save memory
- Because the step size is fixed
- It does not need to