AnyLearn
All lessons
Mathintermediate

Gradient descent: choosing the step and knowing the rate

Gradient descent is three lines of code and a hundred years of theory. This lesson derives why a safe step size is one over the smoothness constant, why the condition number governs everything, and why acceleration reaching order one over k squared is provably the best any first-order method can do.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 11

The method, and the only real decision

Gradient descent is one line repeated:

xk+1=xkηf(xk)x_{k+1} = x_k - \eta \nabla f(x_k)

The gradient points uphill, so you step against it. Everything interesting is in η\eta, the step size.

Too small and you crawl, spending thousands of iterations covering ground a larger step crosses in ten. Too large and you overshoot the valley floor onto the opposite wall, higher than where you started, and the iterates diverge.

What makes this a mathematical question rather than a matter of taste is that the boundary between those regimes is computable. For a convex function with bounded curvature there is a threshold above which divergence is guaranteed and below which decrease is guaranteed, and it depends on exactly one constant.

Full lesson text

All 11 steps on one page, for reading, reference, and search.

Show

1. The method, and the only real decision

Gradient descent is one line repeated:

xk+1=xkηf(xk)x_{k+1} = x_k - \eta \nabla f(x_k)

The gradient points uphill, so you step against it. Everything interesting is in η\eta, the step size.

Too small and you crawl, spending thousands of iterations covering ground a larger step crosses in ten. Too large and you overshoot the valley floor onto the opposite wall, higher than where you started, and the iterates diverge.

What makes this a mathematical question rather than a matter of taste is that the boundary between those regimes is computable. For a convex function with bounded curvature there is a threshold above which divergence is guaranteed and below which decrease is guaranteed, and it depends on exactly one constant.

2. Smoothness gives you a quadratic ceiling

Assume ff is LL-smooth, meaning its gradient is Lipschitz with constant LL, equivalently 2fLI\nabla^2 f \preceq L I. That single assumption produces the descent lemma, the workhorse of the whole theory:

f(y)f(x)+f(x)(yx)+L2yx2f(y) \le f(x) + \nabla f(x)^\top (y - x) + \frac{L}{2}\|y - x\|^2

Read it as a picture. At any point you have the tangent line, and smoothness says the function never rises faster than a fixed quadratic sitting on top of that tangent. You cannot be surprised by more curvature than LL.

Key idea: Convexity gave a lower bound on the function, since the tangent underestimates it. Smoothness gives an upper bound. Squeezed between a linear floor and a quadratic ceiling, the step size stops being guesswork.

3. Deriving the safe step

Put the gradient step y=xηf(x)y = x - \eta \nabla f(x) into the descent lemma:

f(xηf(x))f(x)ηf(x)2+Lη22f(x)2=f(x)η(1Lη2)f(x)2f(x - \eta \nabla f(x)) \le f(x) - \eta \|\nabla f(x)\|^2 + \frac{L \eta^2}{2}\|\nabla f(x)\|^2 = f(x) - \eta\left(1 - \frac{L\eta}{2}\right)\|\nabla f(x)\|^2

The bracket is positive whenever η<2/L\eta < 2/L, so every step strictly decreases ff unless the gradient is already zero. Minimising the guaranteed decrease over η\eta puts the optimum at η=1/L\eta = 1/L, which gives

f(xk+1)f(xk)12Lf(xk)2f(x_{k+1}) \le f(x_k) - \frac{1}{2L}\|\nabla f(x_k)\|^2

So 2/L2/L is where divergence begins and 1/L1/L is the best fixed step the bound can justify. Neither number was tuned; both fell out of one curvature assumption.

4. Two regimes, two very different rates

How fast the error shrinks depends on whether the function is merely convex or strongly convex.

AssumptionRateIterations for accuracy ε\varepsilon
convex, LL-smoothf(xk)f=O(1/k)f(x_k) - f^\star = O(1/k)O(1/ε)O(1/\varepsilon)
μ\mu-strongly convex, LL-smooth(1μL)k\left(1 - \frac{\mu}{L}\right)^k, linearO ⁣(κlog1ε)O\!\left(\kappa \log \frac{1}{\varepsilon}\right)

The gap is enormous. Under plain convexity, ten times the accuracy costs ten times the work. Under strong convexity, error falls by a constant factor every iteration, so each additional decimal digit costs a fixed number of steps.

The price is the condition number κ=L/μ\kappa = L/\mu sitting in front of the logarithm. Strong convexity buys you a fast regime; conditioning decides how fast that fast regime actually is.

5. What conditioning costs, in iterations

Take the strongly convex bound (11/κ)k(1 - 1/\kappa)^k and plot it for a well-conditioned and a badly-conditioned problem.

Error bound over iterations, by condition number
kappa = 10kappa = 100
relative error bound00.20.40.60.810102030405060708090
Source: Computed from the gradient descent bound (1 - 1/kappa)^k

At κ=10\kappa = 10 the error is under one percent by iteration 50. At κ=100\kappa = 100 it has not yet halved. Both problems are convex, both have the theoretical guarantee, and one of them is useless in practice. Conditioning, not convexity, is what people are fighting when optimization runs slowly.

6. Putting a number on badly conditioned

Real problems reach condition numbers far worse than 100.

Predict first

A problem has κ=1000\kappa = 1000. Roughly how many gradient steps buy one extra decimal digit of accuracy? And how many would an accelerated method need?

7. Why conditioning slows the method down

The geometry explains the arithmetic. On a badly conditioned quadratic the level sets are long thin ellipses, and the gradient points nearly perpendicular to the valley floor rather than along it.

So each step crosses the valley rather than travelling down it. The iterates zigzag between the walls, making rapid progress on the steep direction, which was never the bottleneck, and almost none along the shallow direction where the solution actually lies.

Gotcha: This is a property of the parameterisation, not of the underlying problem. Rescaling the variables changes κ\kappa and therefore changes the convergence rate, without changing the geometry of the solution at all. Feature normalisation in machine learning is exactly this trick: it is conditioning work dressed as data preparation.

8. Watching the rate directly

On a diagonal quadratic f(x)=12xAxf(x) = \tfrac{1}{2} x^\top A x the constants are readable off the matrix: LL is the largest eigenvalue, μ\mu the smallest.

import numpy as np

def run(kappa, steps=100):
    A = np.diag([1.0, kappa])        # mu = 1, L = kappa
    x = np.array([1.0, 1.0])
    eta = 1.0 / kappa                # the safe step, 1/L
    for _ in range(steps):
        x = x - eta * (A @ x)
    return 0.5 * x @ A @ x           # f(x), optimal value is 0

for k in (10, 100, 1000):
    print(k, f"{run(k):.3e}")
# 10   1.16e-05
# 100  2.05e-01
# 1000 4.53e-01

The step size is forced down by the largest eigenvalue while progress is governed by the smallest, which is the condition number acting through the algorithm rather than merely appearing in its bound.

9. Acceleration, and the limit of first-order methods

Nesterov's accelerated gradient adds a momentum term, taking the step from a point extrapolated past the current iterate rather than from the iterate itself. The cost per iteration is unchanged: still one gradient.

MethodConvex, LL-smoothμ\mu-strongly convex
Gradient descentO(1/k)O(1/k)O(κlog1ε)O(\kappa \log \tfrac{1}{\varepsilon})
Accelerated gradientO(1/k2)O(1/k^2)O(κlog1ε)O(\sqrt{\kappa} \log \tfrac{1}{\varepsilon})

Key idea: Nesterov also proved a matching lower bound: no method that only queries gradients can beat O(1/k2)O(1/k^2) on this function class. Acceleration is not a clever heuristic that might be improved on later. It is the end of the road for first-order information, and getting past it requires using second derivatives, which is the next lesson.

10. What the theory assumes, and what practice does

Every rate above assumes an exact gradient, a known LL, and a fixed step. Practice violates all three.

  • Gradients are estimated. Stochastic gradient descent uses a minibatch, so the variance floor means a fixed step converges to a neighbourhood of the optimum rather than the optimum, and the step must decay to close that gap.
  • LL is unknown. Backtracking line search shrinks a trial step until the descent lemma is satisfied, recovering the guarantee without ever computing LL.
  • Steps are adapted. Adam and its relatives keep per-coordinate scales, an approximation of the rescaling that conditioning arguments say you want.

In practice: These methods are chosen because they behave well empirically, not because the convex rates transfer. The theory tells you what governs speed. It does not certify the optimiser in your training script.

11. What gradients cannot see

Everything here used one piece of local information: the slope. That is enough to guarantee progress, and the lower bound says it is not enough to do better than O(1/k2)O(1/k^2).

The missing information is curvature. Gradient descent takes the same shaped step in every direction and lets a scalar η\eta mediate, which is why an elongated valley defeats it. A method that knows the Hessian can take a differently shaped step in every direction at once, and stops caring about conditioning entirely.

That is Newton's method, and layering it on top of a barrier function is what turned constrained optimization from a heuristic craft into something with polynomial-time guarantees. Before that, the next lesson in this path covers the constrained machinery those methods rely on: the Lagrangian, duality, and the KKT conditions.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. For an L-smooth convex function, above which step size does the descent guarantee fail?
    • 1/L
    • 2/L
    • L/2
    • 1/(2L)
  2. What does strong convexity change about the convergence of gradient descent?
    • It removes the dependence on the condition number
    • It turns an O(1/k) rate into linear convergence, so each digit costs a fixed number of steps
    • It allows arbitrarily large step sizes without divergence
    • It guarantees convergence in a finite number of steps
  3. A convex problem converges very slowly. Rescaling the input features fixes it. Why?
    • Rescaling makes the objective strongly convex when it was not
    • Rescaling reduces the number of variables the method must handle
    • Rescaling lowers the condition number, which sets the convergence rate
    • Rescaling removes the Lipschitz bound on the gradient
  4. For a strongly convex problem with kappa = 10,000, roughly how does acceleration change the iteration count?
    • It removes the kappa dependence entirely
    • It replaces kappa with its square root, so about 100 instead of 10,000
    • It squares kappa, making the problem harder
    • It halves the iteration count to about 5,000
  5. Why is Nesterov's O(1/k^2) rate described as optimal rather than merely fast?
    • Because no algorithm of any kind can converge faster on convex problems
    • Because a matching lower bound shows no gradient-only method can beat it on that function class
    • Because it matches the cost of computing a single gradient
    • Because it is the fastest rate anyone has implemented so far

Related lessons

Math
advanced

Newton's method and the interior point revolution

Second derivatives buy something gradients cannot: a step shaped by curvature, immune to conditioning, converging quadratically. This lesson builds Newton's method, then layers it on a log barrier to get interior point methods, the machinery that made large constrained problems solvable with a certificate rather than a hope.

13 steps·~20 min
Math
intermediate

Convexity: the property that decides what is solvable

Convexity is what separates optimization problems you can solve with a guarantee from ones you can only hope about. This lesson defines convex sets and functions, proves why every local minimum is global, and gives you the operations that let you recognise convexity without touching a Hessian.

11 steps·~17 min
Math
intermediate

Gradients, Jacobians, and Hessians: Calculus in Many Dimensions

One derivative becomes three objects once a function has many inputs and many outputs. This lesson builds the gradient, the Jacobian and the Hessian, shows what each one actually tells you, and explains why curvature decides how many steps an optimiser needs and why nobody ever writes the Hessian down.

10 steps·~15 min
Math
intermediate

The Derivative Is a Local Linear Model

Machine learning uses the derivative as a search strategy, not a symbolic exercise. This lesson builds it as the best local linear approximation, derives the gradient descent update from it, and shows why estimating derivatives numerically loses half your digits and costs one function evaluation per parameter.

10 steps·~15 min