TExcelTAD
Home › Optimization › Projected Gradient Descent

Projected Gradient Descent

When the feasible set CC is simple, there is a direct way to stay feasible: take a gradient step and then project back onto CC. 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, PC(z)=arg⁡min⁡x∈C∥x−z∥P_C(\mathbf{z}) = \arg\min_{\mathbf{x} \in C}\lVert \mathbf{x} - \mathbf{z} \rVert. The iteration is

xk+1=PC(xk−t∇f(xk))\mathbf{x}_{k+1} = P_C\left(\mathbf{x}_k - t\nabla f(\mathbf{x}_k)\right)

A point is optimal exactly when it is a fixed point, x∗=PC(x∗−t∇f(x∗))\mathbf{x}^* = P_C(\mathbf{x}^* - t\nabla f(\mathbf{x}^*)), which is the variational inequality from Optimality Conditions. With t=1/Lt = 1/L, the rates are the same as for unconstrained gradient descent.

Projections you can compute

Set CC PC(z)P_C(\mathbf{z})
box ℓ≤x≤u\boldsymbol{\ell} \le \mathbf{x} \le \mathbf{u} np.clip(z, l, u)
non-negative orthant max⁡(z,0)\max(\mathbf{z}, \mathbf{0})
Euclidean ball ∥x∥≤r\lVert \mathbf{x} \rVert \le r zmin⁡(1,r/∥z∥)\mathbf{z}\min(1, r/\lVert \mathbf{z} \rVert)
hyperplane aTx=b\mathbf{a}^T\mathbf{x} = b z−aTz−b∥a∥2a\mathbf{z} - \frac{\mathbf{a}^T\mathbf{z} - b}{\lVert \mathbf{a} \rVert^2}\mathbf{a}
probability simplex x≥0,∑xi=1\mathbf{x} \ge 0, \sum x_i = 1 sort-based, O(nlog⁡n)O(n\log n) (below)
affine set Ax=bA\mathbf{x} = \mathbf{b} z−AT(AAT)−1(Az−b)\mathbf{z} - A^T(AA^T)^{-1}(A\mathbf{z} - \mathbf{b})

Sets like {x:Ax≤b}\{\mathbf{x} : A\mathbf{x} \le \mathbf{b}\} in general have no cheap projection: projecting is itself a QP. Then use other methods.

Example: non-negative least squares

python · try it yourself
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 τ\tau, subtract and clip.

python · try it yourself
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:

python · try it yourself
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 ℓ∞\ell_\infty ball around an image.

Exercises

Exercise 1: Project onto a ball

Project z=(3,4)\mathbf{z} = (3, 4) onto the ball of radius 2 centred at the origin.

Answer

(1.2,1.6)(1.2, 1.6).

Solution

∥z∥=5>2\lVert \mathbf{z} \rVert = 5 > 2, so scale by 2/52/5: (1.2,1.6)(1.2, 1.6).

Exercise 2: Fixed point

Minimize (x−3)2(x - 3)^2 over [0,1][0, 1]. Show that x=1x = 1 is a fixed point of projected gradient with any t>0t > 0.

Answer

P(1−t⋅(−4))=P(1+4t)=1P(1 - t \cdot (-4)) = P(1 + 4t) = 1.

Solution

f′(1)=2(1−3)=−4f'(1) = 2(1 - 3) = -4, so the gradient step moves to 1+4t>11 + 4t > 1, and clipping returns 1.

Exercise 3: Affine projection (code)

Project z=(1,2,3)\mathbf{z} = (1, 2, 3) onto the plane x+y+z=3x + y + z = 3 and verify the residual is parallel to the normal.

Answer

(0,1,2)(0, 1, 2).

Solution
python · try it yourself
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.]

aTz=6\mathbf{a}^T\mathbf{z} = 6, so subtract 6−33=1\frac{6 - 3}{3} = 1 from every coordinate. The residual (1,1,1)(1, 1, 1) is the normal direction.

Test yourself

Projected gradient descent needs…

  1. a cheap projection onto the feasible set
  2. the Hessian
  3. integer variables
  4. a non-convex set

The projection of z\mathbf{z} onto the box [0,1]n[0, 1]^n is…

  1. z/∥z∥\mathbf{z}/\lVert \mathbf{z} \rVert
  2. np.clip(z, 0, 1)
  3. max⁡(z)\max(\mathbf{z})
  4. z−mean⁡(z)\mathbf{z} - \operatorname{mean}(\mathbf{z})

A point x∗\mathbf{x}^* is optimal for a convex problem over CC exactly when…

  1. ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0}
  2. x∗=PC(x∗−t∇f(x∗))\mathbf{x}^* = P_C(\mathbf{x}^* - t\nabla f(\mathbf{x}^*))
  3. x∗\mathbf{x}^* is a vertex
  4. f(x∗)=0f(\mathbf{x}^*) = 0