AI Knowledge Tree

Gradient descent

Derive gradient descent from a local model and analyze its descent guarantee.

The negative gradient is the steepest-descent direction; gradient descent simply takes repeated steps along it. It is the algorithm under nearly every model trained today. This lesson derives the update from a local quadratic model, explains the step-size tradeoff, and proves it makes progress on smooth functions.

The update

Gradient descent generates iterates by

xt+1=xt−η ∇f(xt),x_{t+1} = x_t - \eta\, \nabla f(x_t),

where η>0\eta > 0 is the step size (learning rate). Each step moves opposite the gradient, downhill, by an amount scaled by η\eta[1].

Derivation from a local model

The update is the minimizer of a local model of ff. Approximate ff near xtx_t by its first-order Taylor expansion plus a quadratic proximity penalty that distrusts large steps:

f^(x)=f(xt)+∇f(xt)⊤(x−xt)+12η∥x−xt∥2.\hat f(x) = f(x_t) + \nabla f(x_t)^\top (x - x_t) + \frac{1}{2\eta}\lVert x - x_t\rVert^2.

Setting ∇f^(x)=∇f(xt)+1η(x−xt)=0\nabla \hat f(x) = \nabla f(x_t) + \frac{1}{\eta}(x - x_t) = 0 gives x=xt−η ∇f(xt)x = x_t - \eta\,\nabla f(x_t), the gradient step. The step size η\eta is the inverse weight on the trust penalty: small η\eta trusts the linear model only nearby, large η\eta ventures further.

The descent lemma

Progress is guaranteed when the gradient does not change too fast. ff is LL-smooth if its gradient is LL-Lipschitz, ∥∇f(x)−∇f(y)∥≤L∥x−y∥\lVert \nabla f(x) - \nabla f(y)\rVert \le L\lVert x - y\rVert, which for twice- differentiable ff means ∇2f⪯LI\nabla^2 f \preceq L I. Smoothness gives a quadratic upper bound:

f(y)≤f(x)+∇f(x)⊤(y−x)+L2∥y−x∥2.f(y) \le f(x) + \nabla f(x)^\top (y - x) + \frac{L}{2}\lVert y - x\rVert^2.

Descent lemma. Take a gradient step y=x−η∇f(x)y = x - \eta\nabla f(x) and substitute:

f(y)≤f(x)−η∥∇f(x)∥2+Lη22∥∇f(x)∥2=f(x)−η(1−Lη2)∥∇f(x)∥2.f(y) \le f(x) - \eta\lVert \nabla f(x)\rVert^2 + \frac{L\eta^2}{2}\lVert \nabla f(x)\rVert^2 = f(x) - \eta\left(1 - \frac{L\eta}{2}\right)\lVert \nabla f(x)\rVert^2.

For any step size 0<η<2/L0 < \eta < 2/L the bracket is positive, so f(y)<f(x)f(y) < f(x) whenever the gradient is nonzero: the objective strictly decreases. The largest guaranteed per-step decrease is at η=1/L\eta = 1/L, giving f(y)≤f(x)−12L∥∇f(x)∥2f(y) \le f(x) - \frac{1}{2L}\lVert\nabla f(x)\rVert^2[2].

The step-size tradeoff

The descent lemma makes the tradeoff concrete. Too small an η\eta wastes the allowed decrease and crawls. Too large (η≥2/L\eta \ge 2/L) breaks the guarantee: the quadratic penalty term dominates and the iterates can oscillate or diverge. On a quadratic with curvature LL, the threshold η=2/L\eta = 2/L is exactly where the update starts to amplify the error instead of shrinking it.

Numbers fix the scale. The quadratic in the code block below has Hessian AA with rows (3,1)(3, 1) and (1,2)(1, 2), whose eigenvalues are 1.3821.382 and 3.6183.618, so L=3.618L = 3.618 and the safe range is 0<η<2/L=0.5530 < \eta < 2/L = 0.553. Reading the bracket 1−Lη/21 - L\eta/2 across that range:

  • η=0.05\eta = 0.05 (well below the optimum): the step decreases ff but barely, throwing away most of the allowed per-step drop.
  • η=1/L=0.276\eta = 1/L = 0.276: the bracket is 1/21/2, the largest guaranteed decrease, clean convergence.
  • η=0.50\eta = 0.50 (just under 2/L2/L): still convergent, but the bracket has shrunk to 0.0960.096, so each step buys almost nothing and the path oscillates.
  • η=2.5/L=0.691\eta = 2.5/L = 0.691: past the threshold, the bracket is negative, and the iterates blow up to infinity.
Gradient descent on a 2D quadratic: the path to the optimum (left), and how the step size η decides between slow descent, clean convergence at η = 1/L, oscillation, and divergence past η = 2/L (right).
Gradient descent on a 2D quadratic: the path to the optimum (left), and how the step size η decides between slow descent, clean convergence at η = 1/L, oscillation, and divergence past η = 2/L (right).

Check it on arrays

python · numpy
import numpy as np

# Minimize f(x) = 0.5 x^T A x - b^T x, gradient A x - b, minimum at x* = A^{-1} b.
A = np.array([[3.0, 1.0], [1.0, 2.0]])           # symmetric PD
b = np.array([1.0, -1.0])
f = lambda x: 0.5 * x @ A @ x - b @ x
grad = lambda x: A @ x - b
x_star = np.linalg.solve(A, b)

L = np.linalg.eigvalsh(A).max()                  # smoothness = largest eigenvalue
eta = 1.0 / L

x = np.zeros(2)
for t in range(60):
    x = x - eta * grad(x)
print("GD solution:", np.round(x, 6), " exact:", np.round(x_star, 6))

# Monotone decrease at eta = 1/L
x, prev = np.array([5.0, 5.0]), np.inf
decreasing = True
for t in range(50):
    fx = f(x)
    decreasing &= (fx <= prev + 1e-12)
    prev = fx
    x = x - eta * grad(x)
print("monotone decrease at eta=1/L:", decreasing)

# Divergence when eta > 2/L
eta_big = 2.5 / L
x = np.array([5.0, 5.0])
for t in range(40):
    x = x - eta_big * grad(x)
print("eta>2/L diverges:", not np.all(np.isfinite(x)) or np.linalg.norm(x) > 1e6)

Gradient descent at η=1/L\eta = 1/L converges to the exact minimizer with a monotone decrease, and pushing past 2/L2/L diverges, matching the descent lemma.

Exercises

Exercise 1. Derive the gradient-descent update by minimizing the local model f^(x)=f(xt)+∇f(xt)⊤(x−xt)+12η∥x−xt∥2\hat f(x) = f(x_t) + \nabla f(x_t)^\top (x - x_t) + \tfrac{1}{2\eta}\lVert x - x_t\rVert^2.

Solution

Step 1. Take the gradient of f^\hat f in xx. The constant and the linear term contribute ∇f(xt)\nabla f(x_t); the penalty 12η∥x−xt∥2\tfrac{1}{2\eta}\lVert x - x_t\rVert^2 contributes 1η(x−xt)\tfrac{1}{\eta}(x - x_t):

∇f^(x)=∇f(xt)+1η(x−xt).\nabla \hat f(x) = \nabla f(x_t) + \tfrac{1}{\eta}(x - x_t).

Step 2. Set it to zero and solve for xx:

1η(x−xt)=−∇f(xt)  ⟹  x=xt−η ∇f(xt).\tfrac{1}{\eta}(x - x_t) = -\nabla f(x_t) \implies x = x_t - \eta\, \nabla f(x_t).

Step 3. The model f^\hat f is convex (its Hessian is 1ηI≻0\tfrac{1}{\eta} I \succ 0), so this stationary point is the global minimizer. It is the gradient step.

Exercise 2. Starting from the descent lemma f(y)≤f(x)−η(1−Lη2)∥∇f(x)∥2f(y) \le f(x) - \eta(1 - \tfrac{L\eta}{2})\lVert\nabla f(x)\rVert^2, find the step size η\eta that maximizes the guaranteed per-step decrease, and state that decrease.

Solution

Step 1. The guaranteed decrease is η(1−Lη2)∥∇f(x)∥2\eta(1 - \tfrac{L\eta}{2})\lVert\nabla f(x)\rVert^2. Holding the gradient fixed, maximize the scalar coefficient q(η)=η−L2η2q(\eta) = \eta - \tfrac{L}{2}\eta^2 over η>0\eta > 0.

Step 2. Differentiate and set to zero: q′(η)=1−Lη=0q'(\eta) = 1 - L\eta = 0, so η⋆=1/L\eta^\star = 1/L. Since q′′(η)=−L<0q''(\eta) = -L < 0, this is a maximum.

Step 3. Evaluate the bracket at η⋆=1/L\eta^\star = 1/L: 1−L2⋅1L=121 - \tfrac{L}{2}\cdot\tfrac1L = \tfrac12, so the coefficient is 1L⋅12=12L\tfrac{1}{L}\cdot\tfrac12 = \tfrac{1}{2L}. The largest guaranteed decrease is

f(y)≤f(x)−12L∥∇f(x)∥2.f(y) \le f(x) - \tfrac{1}{2L}\lVert\nabla f(x)\rVert^2.

Exercise 3 (code). For the quadratic f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with AA having rows (3,1)(3,1) and (1,2)(1,2) and b=(1,−1)b = (1, -1), scan step sizes across the safe range and verify empirically that the descent lemma bracket 1−Lη/21 - L\eta/2 predicts whether ff keeps decreasing: descent for η<2/L\eta < 2/L, divergence past it.

Solution
python · numpy
import numpy as np

A = np.array([[3.0, 1.0], [1.0, 2.0]])
b = np.array([1.0, -1.0])
f = lambda x: 0.5 * x @ A @ x - b @ x
grad = lambda x: A @ x - b
L = np.linalg.eigvalsh(A).max()

def converges(eta, steps=400):
    x = np.array([5.0, 5.0])
    for _ in range(steps):
        x = x - eta * grad(x)
    return np.all(np.isfinite(x)) and np.linalg.norm(x) < 1e6

# Bracket positive (eta < 2/L) predicts descent; negative predicts blow-up.
for eta in [0.05, 1.0 / L, 0.50, 2.5 / L]:
    bracket = 1 - L * eta / 2
    assert converges(eta) == (bracket > 0)
    print(f"eta={eta:.3f}  bracket={bracket:+.3f}  converges={converges(eta)}")

print("2/L threshold =", round(2 / L, 4))

The bracket sign matches convergence at every tested step size: positive bracket gives a decreasing, convergent run, and the negative bracket past 2/L2/L diverges.

The next lesson quantifies how fast convergence is, using smoothness and strong convexity.


References

  1. [1]Nocedal, J., & Wright, S. J. (2006). Numerical Optimization (2nd ed.), §2.2 and ch. 3. Springer. Steepest descent and the line-search framework (doi:10.1007/978-0-387-40065-5).↩
  2. [2]Boyd, S., & Vandenberghe, L. (2004). Convex Optimization, §9.3. Cambridge University Press. The descent lemma and the step-size analysis for gradient descent.↩