TExcelTAD
Home › Optimization › Subgradients

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 g\mathbf{g} is a subgradient of a convex ff at x\mathbf{x} if the linear function it defines lies below ff everywhere:

f(y)≥f(x)+gT(y−x)for all yf(\mathbf{y}) \ge f(\mathbf{x}) + \mathbf{g}^T(\mathbf{y} - \mathbf{x}) \quad \text{for all } \mathbf{y}

The set of all subgradients is the subdifferential ∂f(x)\partial f(\mathbf{x}). If ff is differentiable at x\mathbf{x}, ∂f(x)={∇f(x)}\partial f(\mathbf{x}) = \{\nabla f(\mathbf{x})\}.

Function Subdifferential
∣x∣\lvert x \rvert {−1}\{-1\} for x<0x < 0, [−1,1][-1, 1] at x=0x = 0, {1}\{1\} for x>0x > 0
max⁡(0,1−z)\max(0, 1 - z) (hinge) {−1}\{-1\} for z<1z < 1, [−1,0][-1, 0] at z=1z = 1, {0}\{0\} for z>1z > 1
∥x∥1\lVert \mathbf{x} \rVert_1 componentwise: sign⁡(xi)\operatorname{sign}(x_i), or [−1,1][-1, 1] where xi=0x_i = 0
max⁡ifi(x)\max_i f_i(\mathbf{x}) convex hull of ∇fi(x)\nabla f_i(\mathbf{x}) over the active ii
∥x∥2\lVert \mathbf{x} \rVert_2 x/∥x∥2\mathbf{x}/\lVert \mathbf{x} \rVert_2 for x≠0\mathbf{x} \ne 0; the unit ball at 0

Optimality

For convex ff, x∗\mathbf{x}^* is a global minimum if and only if

0∈∂f(x∗)\mathbf{0} \in \partial f(\mathbf{x}^*)

For f(x)=∣x∣+12(x−a)2f(x) = \lvert x \rvert + \frac{1}{2}(x - a)^2: 0∈[−1,1]+(0−a)0 \in [-1, 1] + (0 - a) when ∣a∣≤1\lvert a \rvert \le 1, so the minimum is exactly at 0 for every aa in [−1,1][-1, 1]. That is the soft-thresholding effect behind the lasso.

The subgradient method

Replace the gradient with any subgradient: xk+1=xk−tkgk\mathbf{x}_{k+1} = \mathbf{x}_k - t_k\mathbf{g}_k. Two differences from gradient descent:

  • A subgradient step can increase ff, so track the best point seen.
  • A fixed step does not converge to the minimum; use diminishing steps such as tk=c/k+1t_k = c/\sqrt{k + 1}. The rate is O(1/k)O(1/\sqrt{k}), slow, but it works for any convex Lipschitz function.
python · try it yourself
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 λ2∥w∥2+1n∑imax⁡(0,1−yiwTxi)\frac{\lambda}{2}\lVert \mathbf{w} \rVert^2 + \frac{1}{n}\sum_i\max(0, 1 - y_i\mathbf{w}^T\mathbf{x}_i) is strongly convex but not smooth. Pegasos runs stochastic subgradient steps with tk=1/(λk)t_k = 1/(\lambda k):

python · try it yourself
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 ∂f(1)\partial f(1) for f(x)=max⁡(x,2x−1)f(x) = \max(x, 2x - 1).

Answer

[1,2][1, 2].

Solution

At x=1x = 1 both pieces equal 1, so both are active; the subdifferential is the convex hull of their slopes 1 and 2.

Exercise 2: Optimality

Minimize f(x)=∣x−1∣+∣x−2∣+∣x−6∣f(x) = \lvert x - 1 \rvert + \lvert x - 2 \rvert + \lvert x - 6 \rvert using 0∈∂f0 \in \partial f.

Answer

x∗=2x^* = 2, the median.

Solution

For xx between 1 and 6 but not at 2, the slope is (+1)+(±1)+(−1)(+1) + (\pm 1) + (-1), nonzero. At x=2x = 2: ∂f=1+[−1,1]−1=[−1,1]∋0\partial f = 1 + [-1, 1] - 1 = [-1, 1] \ni 0.

Exercise 3: A fixed step stalls (code)

Minimize ∣x∣\lvert x \rvert from x0=1x_0 = 1 with the fixed step t=0.3t = 0.3. What happens?

Answer

The iterates bounce around 0 forever and never get closer than about 0.1.

Solution
python · try it yourself
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 ∣x∣\lvert x \rvert at x=0x = 0 is…

  1. {0}\{0\}
  2. [−1,1][-1, 1]
  3. {−1,1}\{-1, 1\}
  4. empty

For convex ff, x∗\mathbf{x}^* is a minimum if and only if…

  1. ∇f(x∗)\nabla f(\mathbf{x}^*) exists
  2. 0∈∂f(x∗)\mathbf{0} \in \partial f(\mathbf{x}^*)
  3. f(x∗)=0f(\mathbf{x}^*) = 0
  4. ∂f(x∗)\partial f(\mathbf{x}^*) is empty

Why does the subgradient method track the best point seen?

  1. A subgradient step may increase ff
  2. To save memory
  3. Because the step size is fixed
  4. It does not need to