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 , repeat
The negative gradient is the direction of steepest descent in the Euclidean norm: among all unit directions , it minimizes the directional derivative . Any direction with is a descent direction, and part 2 is a tour of better choices of and .
Guarantees
With a fixed step on an -smooth function (see Smoothness, Strong Convexity and Conditioning):
| Assumption | Guarantee after steps |
|---|---|
| bounded below (non-convex) | |
| convex | |
| -strongly convex |
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:
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, falls only by a constant factor per step, here .
Rosenbrock: the price of a curved valley
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 . Methods that use curvature (Newton's Method, BFGS) solve this in tens of steps.
Choosing the step
| Strategy | Pros | Cons |
|---|---|---|
| fixed | simple, guaranteed | needs ; too cautious where curvature is small |
| exact line search | best along the line | expensive except for quadratics |
| backtracking (Armijo) | no 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 to ; see Momentum and Nesterov.
Exercises
Exercise 1: Descent direction
At a point with , is a descent direction? What about ?
Answer
is; is not.
Solution
is for the first and for the second.
Exercise 2: Steps for a digit
With and the bound , about how many steps reduce the squared distance by a factor of 10?
Answer
About 230.
Solution
Solve : .
Exercise 3: Exact step on a quadratic (code)
Derive the exact step by minimizing for , then verify that consecutive gradients in the example are orthogonal.
Answer
with ; consecutive gradients have dot product 0.
Solution
has , and gives the formula. At the minimizer, .
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_new0.0
0.0
0.0
Test yourself
For a strongly convex, smooth , gradient descent with step converges…
- sublinearly, like
- linearly, with factor
- quadratically
- not at all
Why does gradient descent zig-zag in a narrow valley?
- The gradient is wrong
- The steepest direction points across the valley, not along it
- The step size is zero
- The function is concave
A direction is a descent direction at if…