AI Wiki中文/EN

Gradient descent

梯度下降

Derive a descent step, identify a stable learning-rate interval, and distinguish a small gradient from a global optimum.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

The direction can be right while the step is wrong

Consider a bowl with objective f(x,y)=(x2+9y2)/2f(x,y)=(x^2+9y^2)/2. At (2,2)(2,2) its value is 20 and its gradient is (2,18)(2,18). A learning rate of 0.1 moves to (1.8,0.2)(1.8,0.2) and lowers the objective to 1.8. A learning rate of 0.3 moves to (1.4,−3.4)(1.4,-3.4) and raises it to 53.

Both updates follow the negative gradient. The second overshoots across the steep direction of the bowl. This separates two decisions that beginners often combine: choosing a locally promising direction and choosing how far to move in that direction.

Why the negative gradient is a descent direction

Let ff be a differentiable scalar function of a parameter column vector θ\theta. For a unit direction uu, its directional derivative is ∇f(θ)⊤u\nabla f(\theta)^\top u. By the Cauchy–Schwarz inequality, this is smallest at u=−∇f/∥∇f∥2u=-\nabla f/\|\nabla f\|_2 when the gradient is nonzero. This is steepest local descent measured using Euclidean distance. Changing the geometry can change the steepest direction. Deep Learning, Section 4.3

Gradient descent includes the gradient’s magnitude in its step:

θt+1=θt−η∇f(θt),η>0.\theta_{t+1}=\theta_t-\eta\nabla f(\theta_t),\qquad \eta>0.

A local first-order approximation motivates the direction but does not justify arbitrarily large steps. To bound the change, suppose L>0L>0 and the gradient is LL-Lipschitz along the proposed segment: its change in Euclidean norm is at most LL times the parameter displacement. Integrating the gradient along displacement dd bounds the Taylor remainder:

f(θ+d)−f(θ)−∇f(θ)⊤d=∫01[∇f(θ+td)−∇f(θ)]⊤d dt≤L2∥d∥22.f(\theta+d)-f(\theta)-\nabla f(\theta)^\top d=\int_0^1[\nabla f(\theta+td)-\nabla f(\theta)]^\top d\,dt\le\frac L2\|d\|_2^2.

Substitute d=−η∇f(θ)d=-\eta\nabla f(\theta) to obtain:

f(θ−η∇f(θ))≤f(θ)−η(1−Lη2)∥∇f(θ)∥22.f(\theta-\eta\nabla f(\theta))\le f(\theta)-\eta\left(1-\frac{L\eta}{2}\right)\|\nabla f(\theta)\|_2^2.

Thus 0<η<2/L0<\eta<2/L guarantees a decrease at a nonstationary point under this smoothness condition. This derivation supplies an explicit condition for the step-size problem illustrated in D2L, Section 12.3. It is a sufficient condition, not a requirement that every practical loss has a known global LL.

For our quadratic, the two coordinates evolve independently:

xt+1=(1−η)xt,yt+1=(1−9η)yt.x_{t+1}=(1-\eta)x_t,\qquad y_{t+1}=(1-9\eta)y_t.

Both coordinates converge to zero from any start precisely when 0<η<2/90<\eta<2/9. At η=2/9\eta=2/9, the yy coordinate alternates sign without shrinking. Unequal curvatures make one learning rate a compromise. The same narrow-valley difficulty appears in the optimization discussion in MML, Section 7.1.

Keep the trajectory so you can inspect it

This complete Python program specializes the update to our quadratic. It records parameters and objective values, tests the first update by hand, checks the predicted stability boundary, and compares the analytic gradient with finite differences.

import math

def objective(x, y):
    return (x*x+9*y*y)/2

def descend(rate, steps, start=(2.0, 2.0)):
    if not math.isfinite(rate) or rate <= 0 or type(steps) is not int or steps < 0:
        raise ValueError("positive finite rate and nonnegative integer steps required")
    x, y = start
    if not all(math.isfinite(v) for v in (x, y)):
        raise ValueError("finite starting coordinates required")
    initial_value = objective(x,y)
    if not math.isfinite(initial_value):
        raise ArithmeticError("nonfinite initial objective")
    trace = [(x, y, initial_value)]
    for _ in range(steps):
        x, y = x-rate*x, y-rate*9*y
        value = objective(x,y)
        if not all(math.isfinite(v) for v in (x,y,value)):
            raise ArithmeticError("nonfinite trajectory")
        trace.append((x,y,value))
    return trace

trace = descend(0.1, 100)
assert math.isclose(trace[1][2], 1.8)
assert all(b[2] <= a[2] for a,b in zip(trace,trace[1:]))
assert trace[-1][2] < 1e-8
assert math.isclose(descend(0.3,1)[1][2], 53)
assert math.isclose(abs(descend(2/9,20)[-1][1]), 2)
assert abs(descend(1/9,1)[1][1]) < 1e-12
assert descend(0.1,0) == [(2.0,2.0,20.0)]
h = 1e-5
for x,y in [(2.0,2.0),(-1.0,0.5),(0.0,0.0)]:
    dx = (objective(x+h,y)-objective(x-h,y))/(2*h)
    dy = (objective(x,y+h)-objective(x,y-h))/(2*h)
    assert abs(dx-x) < 1e-8 and abs(dy-9*y) < 1e-8

For a general dd-parameter objective, TT steps cost O(T(Cg+d))O(T(C_g+d)) if computing the gradient costs CgC_g. Logging the objective adds O(TCf)O(TC_f) work. A full trajectory uses O(Td)O(Td) storage; keeping only the current iterate requires O(d)O(d) working storage beyond the objective and gradient computations.

A stopping rule does not certify everything

A zero gradient can occur at a local minimum, a maximum, or a saddle. For example, x2−y2x^2-y^2 has zero gradient at the origin while nearby values have both signs. Small parameter changes can also result from a very small learning rate rather than a satisfactory solution.

With a fixed valid step, a global smoothness bound, and an objective bounded below, summing the descent inequality shows the squared gradient norms have a finite sum, so those norms tend to zero. This alone does not prove that the parameter sequence converges to a global minimum. Convexity and stronger assumptions support additional conclusions; state them when claiming a convergence rate.

Here each update uses the exact gradient of the specified objective. Mini-batch stochastic gradients introduce sampling noise, so individual steps need not lower the full objective. Momentum, preconditioning, and adaptive methods also change the update. A falling training loss still says nothing by itself about held-out performance.

Diagnose the update

  1. What happens at learning rate 1/91/9 in the quadratic? Answer: yy reaches zero in one step; xx is multiplied by 8/98/9 each step.
  2. Does doubling the objective leave the same learning rate equally safe? Answer: no. It doubles the gradient and curvature bound; halving the learning rate preserves the original updates.
  3. Why does a negative gradient fail to guarantee improvement at rate 0.3 here? Answer: the finite step overshoots; 0.3 exceeds the stability boundary 2/92/9.
  4. What should you log besides training loss? Answer: gradient norms, step sizes, nonfinite values, and task-appropriate validation metrics. These diagnose different problems rather than certifying one universal success condition.

Geometry explains several optimizer design choices

The steep direction in this example restricts progress along the shallow direction. Feature scaling, diagonal preconditioning, and second-order methods address related geometry, although their costs and guarantees differ. Before replacing an optimizer, reproduce a small update manually, verify the gradient, and inspect the learning rate. Those checks can distinguish an optimization difficulty from a coding error.

Make a prediction, then check

For f=(x²+9y²)/2, which fixed learning-rate interval gives convergence to zero from every start?

Choose one answer

Learning objectives

  • Derive descent conditions using directional derivatives and a smoothness bound.
  • Calculate a quadratic objective’s stable learning rates and distinguish stationarity from global optimality.

Prerequisites & connections

Explore related knowledge →

References

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. Dive into Deep Learning — Gradient Descent
    Aston Zhang; Zachary C. Lipton; Mu Li; Alexander J. Smola · 2023
  3. Deep Learning — Chapter 4: Numerical Computation
    Ian Goodfellow; Yoshua Bengio; Aaron Courville · 2016

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.

  • Re-derived the descent bound using a segment integral and Lipschitz gradients; recalculated 20→1.8 and 20→53, the 0<η<2/9 stability interval, and boundary oscillation.
  • Ran analytic versus finite-difference gradients, zero steps, and one-step elimination of y; distinguished stationarity, parameter convergence, and global optimality.
  • Compared arguments, modeling assumptions, numbers, and exercise answers across languages; structural checks separately compare display formulas, code, references, and main sections. No independent human peer review.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.