TExcelTAD
Home › Optimization › Gradient Descent Revisited

Gradient Descent Revisited

Gradient descent was introduced in Math for ML: Gradient Descent as "step downhill". Here we look at it as an optimizer: what it guarantees, how the step size and the condition number control its speed, and why it is the baseline that every other method in this part tries to beat.

The method

Starting from x0\mathbf{x}_0, repeat

xk+1=xk−tk∇f(xk)\mathbf{x}_{k+1} = \mathbf{x}_k - t_k\nabla f(\mathbf{x}_k)

The negative gradient is the direction of steepest descent in the Euclidean norm: among all unit directions d\mathbf{d}, it minimizes the directional derivative ∇f(x)Td\nabla f(\mathbf{x})^T\mathbf{d}. Any direction with ∇f(x)Td<0\nabla f(\mathbf{x})^T\mathbf{d} < 0 is a descent direction, and part 2 is a tour of better choices of d\mathbf{d} and tt.

Guarantees

With a fixed step t=1/Lt = 1/L on an LL-smooth function (see Smoothness, Strong Convexity and Conditioning):

Assumption Guarantee after kk steps
ff bounded below (non-convex) min⁡i<k∥∇f(xi)∥2≤2L(f(x0)−f∗)k\min_{i < k}\lVert \nabla f(\mathbf{x}_i) \rVert^2 \le \frac{2L(f(\mathbf{x}_0) - f^*)}{k}
ff convex f(xk)−f∗≤L∥x0−x∗∥22kf(\mathbf{x}_k) - f^* \le \frac{L\lVert \mathbf{x}_0 - \mathbf{x}^* \rVert^2}{2k}
ff μ\mu-strongly convex ∥xk−x∗∥2≤(1−μ/L)k∥x0−x∗∥2\lVert \mathbf{x}_k - \mathbf{x}^* \rVert^2 \le (1 - \mu/L)^k\lVert \mathbf{x}_0 - \mathbf{x}^* \rVert^2

The first row is all you get in deep learning: gradient descent finds an approximately stationary point. The last row is linear convergence: a fixed number of steps per extra digit.

Watching the zig-zag

On an ill-conditioned quadratic, each exact step is perpendicular to the previous one, and the path zig-zags across the valley:

python · try it yourself
import numpy as np

Q = np.diag([1.0, 20.0])
x = np.array([20.0, 1.0])
path = [x.copy()]
for _ in range(6):
    g = Q @ x
    t = (g @ g) / (g @ Q @ g)              # exact line search for a quadratic
    x = x - t * g
    path.append(x.copy())
for k, p in enumerate(path):
    print(k, np.round(p, 4), " f =", round(0.5 * p @ Q @ p, 4))
0 [20.  1.]  f = 210.0
1 [18.0952 -0.9048]  f = 171.9048
2 [16.3719  0.8186]  f = 140.7202
3 [14.8127 -0.7406]  f = 115.1927
4 [13.4019  0.6701]  f = 94.2961
5 [12.1256 -0.6063]  f = 77.1902
6 [10.9707  0.5485]  f = 63.1875

Even with the best possible step in each direction, ff falls only by a constant factor per step, here (κ−1κ+1)2=(19/21)2≈0.82\left(\frac{\kappa - 1}{\kappa + 1}\right)^2 = (19/21)^2 \approx 0.82.

Rosenbrock: the price of a curved valley

python · try it yourself
import numpy as np

f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
grad = lambda p: np.array([-2 * (1 - p[0]) - 400 * p[0] * (p[1] - p[0]**2), 200 * (p[1] - p[0]**2)])
x = np.array([-1.2, 1.0])
for k in range(1, 20001):
    x = x - 0.001 * grad(x)
    if k in (10, 100, 1000, 10000, 20000):
        print(f"step {k:5d}: x = {np.round(x, 4)}, f = {f(x):.2e}")
step    10: x = [-1.0214  1.0513], f = 4.09e+00
step   100: x = [-0.9482  0.9071], f = 3.80e+00
step  1000: x = [0.3273 0.104 ], f = 4.54e-01
step 10000: x = [0.9924 0.9847], f = 5.85e-05
step 20000: x = [0.9999 0.9997], f = 1.94e-08

The method reaches the valley floor within a few dozen steps, then needs thousands to follow it to (1,1)(1, 1). Methods that use curvature (Newton's Method, BFGS) solve this in tens of steps.

Choosing the step

Strategy Pros Cons
fixed t=1/Lt = 1/L simple, guaranteed needs LL; too cautious where curvature is small
exact line search best along the line expensive except for quadratics
backtracking (Armijo) no LL needed, cheap, guaranteed a few extra function evaluations
decreasing schedule needed for noisy gradients slow on deterministic problems

The next lesson builds backtracking line search, the standard choice.

In software

  • Plain full-batch gradient descent is rarely used directly in libraries; it is the building block for SGD (Stochastic Gradient Descent), projected and proximal methods.
  • Momentum and Nesterov acceleration improve the convex rate from O(1/k)O(1/k) to O(1/k2)O(1/k^2); see Momentum and Nesterov.

Exercises

Exercise 1: Descent direction

At a point with ∇f=(3,−4)\nabla f = (3, -4), is d=(−1,0)\mathbf{d} = (-1, 0) a descent direction? What about (0,−1)(0, -1)?

Answer

(−1,0)(-1, 0) is; (0,−1)(0, -1) is not.

Solution

∇fTd\nabla f^T\mathbf{d} is −3<0-3 < 0 for the first and 4>04 > 0 for the second.

Exercise 2: Steps for a digit

With κ=100\kappa = 100 and the bound (1−1/κ)k(1 - 1/\kappa)^k, about how many steps reduce the squared distance by a factor of 10?

Answer

About 230.

Solution

Solve (0.99)k=0.1(0.99)^k = 0.1: k=ln⁡0.1/ln⁡0.99≈229.1k = \ln 0.1 / \ln 0.99 \approx 229.1.

Exercise 3: Exact step on a quadratic (code)

Derive the exact step t=gTggTQgt = \frac{\mathbf{g}^T\mathbf{g}}{\mathbf{g}^TQ\mathbf{g}} by minimizing ϕ(t)=f(x−tg)\phi(t) = f(\mathbf{x} - t\mathbf{g}) for f=12xTQxf = \frac{1}{2}\mathbf{x}^TQ\mathbf{x}, then verify that consecutive gradients in the example are orthogonal.

Answer

ϕ′(t)=−gTQx+t gTQg=0\phi'(t) = -\mathbf{g}^TQ\mathbf{x} + t\,\mathbf{g}^TQ\mathbf{g} = 0 with Qx=gQ\mathbf{x} = \mathbf{g}; consecutive gradients have dot product 0.

Solution

ϕ(t)=12(x−tg)TQ(x−tg)\phi(t) = \frac{1}{2}(\mathbf{x} - t\mathbf{g})^TQ(\mathbf{x} - t\mathbf{g}) has ϕ′(t)=−gTQx+t gTQg\phi'(t) = -\mathbf{g}^TQ\mathbf{x} + t\,\mathbf{g}^TQ\mathbf{g}, and Qx=gQ\mathbf{x} = \mathbf{g} gives the formula. At the minimizer, ϕ′(t)=−∇f(xk+1)Tgk=0\phi'(t) = -\nabla f(\mathbf{x}_{k+1})^T\mathbf{g}_k = 0.

python · try it yourself
import numpy as np
Q = np.diag([1.0, 20.0]); x = np.array([20.0, 1.0])
g_old = Q @ x
for _ in range(3):
    t = (g_old @ g_old) / (g_old @ Q @ g_old)
    x = x - t * g_old
    g_new = Q @ x
    print(round(float(g_old @ g_new), 10) + 0)
    g_old = g_new
0.0
0.0
0.0

Test yourself

For a strongly convex, smooth ff, gradient descent with step 1/L1/L converges…

  1. sublinearly, like 1/k1/k
  2. linearly, with factor 1−μ/L1 - \mu/L
  3. quadratically
  4. not at all

Why does gradient descent zig-zag in a narrow valley?

  1. The gradient is wrong
  2. The steepest direction points across the valley, not along it
  3. The step size is zero
  4. The function is concave

A direction d\mathbf{d} is a descent direction at x\mathbf{x} if…

  1. ∇f(x)Td<0\nabla f(\mathbf{x})^T\mathbf{d} < 0
  2. ∇f(x)Td>0\nabla f(\mathbf{x})^T\mathbf{d} > 0
  3. ∥d∥=1\lVert \mathbf{d} \rVert = 1
  4. d=∇f(x)\mathbf{d} = \nabla f(\mathbf{x})