Gradient descent
梯度下降
Derive a descent step, identify a stable learning-rate interval, and distinguish a small gradient from a global optimum.
gradient-descentThe direction can be right while the step is wrong
Consider a bowl with objective . At its value is 20 and its gradient is . A learning rate of 0.1 moves to and lowers the objective to 1.8. A learning rate of 0.3 moves to 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 be a differentiable scalar function of a parameter column vector . For a unit direction , its directional derivative is . By the Cauchy–Schwarz inequality, this is smallest at 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:
A local first-order approximation motivates the direction but does not justify arbitrarily large steps. To bound the change, suppose and the gradient is -Lipschitz along the proposed segment: its change in Euclidean norm is at most times the parameter displacement. Integrating the gradient along displacement bounds the Taylor remainder:
Substitute to obtain:
Thus 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 .
For our quadratic, the two coordinates evolve independently:
Both coordinates converge to zero from any start precisely when . At , the 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 -parameter objective, steps cost if computing the gradient costs . Logging the objective adds work. A full trajectory uses storage; keeping only the current iterate requires 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, 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
- What happens at learning rate in the quadratic? Answer: reaches zero in one step; is multiplied by each step.
- 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.
- 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 .
- 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?
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
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Dive into Deep Learning — Gradient Descent
Aston Zhang; Zachary C. Lipton; Mu Li; Alexander J. Smola · 2023 - 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:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §7.1, PDF pp.234–236: negative-gradient update, learning rate, and narrow valleys; translated row-gradient convention to column vectors
- Dive into Deep Learning — Gradient Descent · §12.3: local Taylor motivation and overshoot
- Deep Learning — Chapter 4: Numerical Computation · §4.3: Euclidean directional derivatives and stationary points
Related topics