Projected Gradient Descent
When the feasible set is simple, there is a direct way to stay feasible: take a gradient step and then project back onto . Projected gradient descent keeps the simplicity and the convergence guarantees of gradient descent, and it is how bounds, simplex constraints and norm balls are handled in large-scale ML.
The method
The Euclidean projection onto a closed convex set is the closest point, . The iteration is
A point is optimal exactly when it is a fixed point, , which is the variational inequality from Optimality Conditions. With , the rates are the same as for unconstrained gradient descent.
Projections you can compute
| Set | |
|---|---|
| box | np.clip(z, l, u) |
| non-negative orthant | |
| Euclidean ball | |
| hyperplane | |
| probability simplex | sort-based, (below) |
| affine set |
Sets like in general have no cheap projection: projecting is itself a QP. Then use other methods.
Example: non-negative least squares
import numpy as np
from scipy.optimize import nnls
rng = np.random.default_rng(0)
A = rng.normal(size=(30, 5)); x_true = np.array([1.0, 0.0, 2.0, 0.0, 0.5])
b = A @ x_true + 0.1 * rng.normal(size=30)
L = np.linalg.norm(A, 2)**2
x = np.zeros(5)
for k in range(500):
x = np.maximum(x - (A.T @ (A @ x - b)) / L, 0) # gradient step, then clip at 0
print("projected gradient:", np.round(x, 4) + 0)
print("scipy nnls: ", np.round(nnls(A, b)[0], 4) + 0)projected gradient: [1.0309 0.0095 1.9753 0. 0.5056]
scipy nnls: [1.0309 0.0095 1.9753 0. 0.5056]
The fourth coordinate ends exactly at zero: its bound is active. The second is small but positive, so the data really prefer a little of it.
Projection onto the probability simplex
Many ML problems constrain weights to a probability distribution (mixture weights, portfolio fractions, attention-like allocations). The projection has a short algorithm: sort, find a threshold , subtract and clip.
import numpy as np
def project_simplex(z):
u = np.sort(z)[::-1]
css = np.cumsum(u)
k = np.nonzero(u * np.arange(1, len(z) + 1) > css - 1)[0][-1]
tau = (css[k] - 1) / (k + 1)
return np.maximum(z - tau, 0)
z = np.array([0.8, 0.6, -0.2, 0.1])
p = project_simplex(z)
print("projection:", np.round(p, 4) + 0, " sum:", round(float(p.sum()), 10))
# check: no other simplex point is closer
rng = np.random.default_rng(1)
others = rng.dirichlet(np.ones(4), size=20000)
print("closest random simplex point is farther:", bool(np.linalg.norm(others - z, axis=1).min() >= np.linalg.norm(p - z)))projection: [0.6 0.4 0. 0. ] sum: 1.0
closest random simplex point is farther: True
Example: minimizing over the simplex
Find the mixture of three fixed forecasts that minimizes squared error, with weights on the simplex:
import numpy as np
def project_simplex(z):
u = np.sort(z)[::-1]; css = np.cumsum(u)
k = np.nonzero(u * np.arange(1, len(z) + 1) > css - 1)[0][-1]
return np.maximum(z - (css[k] - 1) / (k + 1), 0)
rng = np.random.default_rng(2)
truth = np.sin(np.linspace(0, 6, 50))
F = np.column_stack([truth + 0.3 * rng.normal(size=50), 0.5 * truth, truth + 0.8]) # three forecasts
w = np.ones(3) / 3
L = np.linalg.norm(F, 2)**2
for _ in range(2000):
w = project_simplex(w - F.T @ (F @ w - truth) / L)
print("weights:", np.round(w, 4) + 0, " MSE:", round(float(np.mean((F @ w - truth)**2)), 5))weights: [0.5523 0.3758 0.0719] MSE: 0.04838
In software
- L-BFGS-B handles boxes with a projected-gradient-style active-set phase; PyTorch users often "clamp after the optimizer step" (
p.data.clamp_(0, 1)), which is projected SGD. - Adversarial-example attacks (PGD) maximize a loss with projected gradient steps onto an ball around an image.
Exercises
Exercise 1: Project onto a ball
Project onto the ball of radius 2 centred at the origin.
Answer
.
Solution
, so scale by : .
Exercise 2: Fixed point
Minimize over . Show that is a fixed point of projected gradient with any .
Answer
.
Solution
, so the gradient step moves to , and clipping returns 1.
Exercise 3: Affine projection (code)
Project onto the plane and verify the residual is parallel to the normal.
Answer
.
Solution
import numpy as np
z = np.array([1.0, 2.0, 3.0]); a = np.ones(3); b = 3.0
p = z - (a @ z - b) / (a @ a) * a
print(p, z - p)[0. 1. 2.] [1. 1. 1.]
, so subtract from every coordinate. The residual is the normal direction.
Test yourself
Projected gradient descent needs…
- a cheap projection onto the feasible set
- the Hessian
- integer variables
- a non-convex set
The projection of onto the box is…
np.clip(z, 0, 1)
A point is optimal for a convex problem over exactly when…
- is a vertex