Gradient Descent

Imagine standing on a foggy hillside, unable to see more than a few feet. You are trying to reach the valley floor. You cannot see the global shape of the terrain. But you can feel the slope under your feet. The natural strategy is to take a step in the steepest downhill direction. Then you reassess and repeat. This is precisely the logic of gradient descent. It is the workhorse optimization algorithm underlying nearly all of modern machine learning. Gradient descent matters because most learning problems reduce to minimizing a loss function over model parameters. Closed-form solutions rarely exist once models grow beyond linear regression. Training a neural network, fitting a logistic regression classifier, or tuning a support vector machine all involve the same challenge. Each requires searching a high-dimensional parameter space for a point where the loss is small. Gradient descent gives a simple, general-purpose recipe. Compute the gradient of the loss, move against it, and repeat. Continue until the improvements become negligible. This article focuses on the clean, foundational case: batch gradient descent applied to convex objectives. Here, every training example contributes to each update. The loss surface has no misleading local minima. Understanding this case rigorously builds the exact intuition needed for more practical variants used at scale. This includes its update rule, its geometric meaning, and its guaranteed convergence rate. By the end, you will be able to derive the update rule from first principles. You will state and prove a convergence rate for convex and smooth objectives. You will trace a full numerical example by hand. You will also recognize why practitioners eventually move to stochastic and momentum-based variants. This sets up Stochastic Gradient Descent and Momentum Methods as refinements of the same core idea.

Key takeaways
  • Gradient descent minimizes a differentiable function by repeatedly stepping in the direction of the negative gradient.
  • The update rule is xt+1=xt−η∇f(xt)x_{t+1} = x_t - \eta \nabla f(x_t), where η\eta is the learning rate.
  • On convex, LL-smooth objectives with a suitable fixed step size, gradient descent converges to the global minimum.
  • Convergence rate is O(1/T)O(1/T) for convex smooth functions and linear (geometric) for strongly convex functions.
  • Batch gradient descent computes the full gradient over all data per step — accurate but computationally heavy.
  • The learning rate controls a fundamental tradeoff between speed and stability; too large diverges, too small stalls.
The core insight

Gradient descent iteratively moves parameters opposite the gradient of a loss function, and on convex smooth objectives this simple rule provably converges to the global minimum at a rate no worse than O(1/T)O(1/T).

The idea

Consider a differentiable function f:Rn→Rf: \mathbb{R}^n \to \mathbb{R} that we want to minimize — in machine learning, ff is typically an empirical loss function of model parameters x∈Rnx \in \mathbb{R}^n averaged over a training set. The gradient ∇f(x)\nabla f(x) is the vector of partial derivatives, and it points in the direction of steepest increase of ff at xx. It follows immediately that −∇f(x)-\nabla f(x) points in the direction of steepest decrease, which is the entire justification for the algorithm's core move.

Gradient descent is the iterative scheme

xt+1=xt−η ∇f(xt),x_{t+1} = x_t - \eta \, \nabla f(x_t),

where η>0\eta > 0 is the learning rate (or step size), and t=0,1,2,…t = 0, 1, 2, \dots indexes iterations starting from some initial guess x0x_0. In the specific case called batch gradient descent, when f(x)=1m∑i=1mfi(x)f(x) = \frac{1}{m}\sum_{i=1}^{m} f_i(x) is an average loss over mm training examples, the gradient ∇f(xt)=1m∑i∇fi(xt)\nabla f(x_t) = \frac{1}{m}\sum_i \nabla f_i(x_t) is computed using all mm examples at every single step. This is what distinguishes it from Stochastic Gradient Descent, which approximates the same gradient using a single example or small mini-batch, trading accuracy for speed. The algorithm's correctness on convex problems rests on a key fact from Convex Functions and Convex Sets: for a convex differentiable function, every point where ∇f(x)=0\nabla f(x) = 0 is a global minimizer, not merely a local one. This means gradient descent's stopping criterion — a vanishing gradient — is exactly the right signal to chase, with no risk of settling into a spurious local trap. The rate at which the algorithm approaches this minimizer depends on two further structural properties: smoothness (a bound on how fast the gradient itself can change, formalized via a Lipschitz constant LL) and, optionally, strong convexity (a lower-curvature bound μ\mu). These two constants govern the choice of learning rate and the resulting convergence rate, discussed in full below.

How it works

The mechanism is deliberately simple: evaluate the gradient, take a scaled step against it, check for convergence, repeat. The entire complexity of applying gradient descent well lies in choosing η\eta and in evaluating ∇f\nabla f efficiently, not in the control flow itself. ``` Algorithm: Batch Gradient Descent Input: differentiable convex objective f, learning rate η, initial point x0, tolerance ε, max iterations T_max Output: approximate minimizer x 1. x ← x0 2. for t = 0, 1, 2, ..., T_max - 1: 3. g ← ∇f(x) # full-batch gradient: average over ALL training points 4. if ‖g‖ < ε: 5. return x # gradient ~0 ⇒ (near) global minimum, by convexity 6. x ← x - η g # step against steepest ascent direction 7. return x # ran out of iterations; may not have fully converged ``` For a supervised learning loss f(x)=1m∑i=1mℓ(x;ai,bi)f(x) = \frac{1}{m}\sum_{i=1}^m \ell(x; a_i, b_i) over data pairs (ai,bi)(a_i, b_i), line 3 expands into a full pass over the dataset: ``` Compute full-batch gradient: 1. g ← 0-vector of length n 2. for i = 1 to m: 3. g ← g + ∇_x ℓ(x; a_i, b_i) 4. g ← g / m 5. return g ``` Two practical refinements are worth stating precisely, because they anticipate later material without changing the core idea: 1. Fixed vs. decaying step size. A constant η\eta suffices for convergence guarantees on smooth convex functions, but many implementations decay ηt\eta_t over time (e.g., ηt=η0/t\eta_t = \eta_0 / \sqrt{t}) to squeeze out faster practical convergence, foreshadowing the deeper analysis in Learning Rates and Convergence. 2. Convergence check. In practice, stopping is triggered by ∥∇f(xt)∥<ε\|\nabla f(x_t)\| < \varepsilon, by a small relative change in ff, or simply by a fixed iteration budget — the pseudocode above shows the gradient-norm criterion because it connects directly to the optimality condition of convex functions.

Figure
One iteration of gradient descent on a convex bowl: gradient direction, step, and the resulting descent
x* (minimum) x_t ∇f(x_t) : steepest ascent -η∇f(x_t) step x_{t+1} Level sets = constant-loss contours of a convex f(x). Each step moves against the gradient, toward x*.

Complexity & correctness

Per-iteration cost. For batch gradient descent on mm training examples with nn parameters, computing the full gradient costs O(mn)O(mn) time in the typical case where each per-example gradient ∇xℓ(x;ai,bi)\nabla_x \ell(x; a_i, b_i) costs O(n)O(n) (e.g., linear models) — more generally O(m⋅C)O(m \cdot C) where CC is the cost of one example's gradient. Space is O(n)O(n) for storing xx and gg, plus O(mn)O(mn) if the dataset itself must reside in memory (often unavoidable regardless of the optimizer). This per-step cost is the central practical drawback of the batch method: every single update requires a full pass over the data, which is why Stochastic Gradient Descent exists — it estimates the same gradient direction from a subsample at a fraction of the cost per step.

Number of iterations to reach accuracy ϵ\epsilon. This is where convexity and smoothness earn their keep. Assume:

  • ff is convex and differentiable.
  • ff is LL-smooth: ∥∇f(x)−∇f(y)∥≤L∥x−y∥\|\nabla f(x) - \nabla f(y)\| \le L \|x - y\| for all x,yx, y (the gradient doesn't change too fast).
  • A global minimizer x∗x^* exists with f∗=f(x∗)f^* = f(x^*).

Claim (convex, smooth case). With fixed step size η=1/L\eta = 1/L, gradient descent satisfies

f(xT)−f∗  ≤  L ∥x0−x∗∥22T.f(x_T) - f^* \;\le\; \frac{L \, \|x_0 - x^*\|^2}{2T}.

Proof sketch. LL-smoothness gives the quadratic upper bound (the descent lemma):

f(y)≤f(x)+∇f(x)⊤(y−x)+L2∥y−x∥2.f(y) \le f(x) + \nabla f(x)^\top (y - x) + \frac{L}{2}\|y - x\|^2.

Substituting y=xt+1=xt−η∇f(xt)y = x_{t+1} = x_t - \eta \nabla f(x_t) and using η=1/L\eta = 1/L:

f(xt+1)≤f(xt)−12L∥∇f(xt)∥2.f(x_{t+1}) \le f(x_t) - \frac{1}{2L}\|\nabla f(x_t)\|^2.

So the objective is guaranteed to strictly decrease at every step (this is the descent property, the invariant underlying correctness). Convexity gives the supporting-hyperplane inequality f(xt)≤f∗+∇f(xt)⊤(xt−x∗)f(x_t) \le f^* + \nabla f(x_t)^\top(x_t - x^*), which after algebraic manipulation with the previous bound produces a telescoping sum:

∑t=0T−1(f(xt+1)−f∗)≤L2∥x0−x∗∥2.\sum_{t=0}^{T-1} \big(f(x_{t+1}) - f^*\big) \le \frac{L}{2}\|x_0 - x^*\|^2.

Since f(xt)f(x_t) is non-increasing (from the descent property), the minimum term f(xT)−f∗f(x_T) - f^* is at most the average of the sum, giving the stated O(1/T)O(1/T) rate. ■\blacksquare

Claim (strongly convex case). If additionally ff is μ\mu-strongly convex (f(y)≥f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2f(y) \ge f(x) + \nabla f(x)^\top(y-x) + \frac{\mu}{2}\|y-x\|^2), then with η=1/L\eta = 1/L,

∥xT−x∗∥2≤(1−μL)T∥x0−x∗∥2,\|x_T - x^*\|^2 \le \left(1 - \frac{\mu}{L}\right)^{T} \|x_0 - x^*\|^2,

a linear (geometric) convergence rate — exponentially fast in TT, governed by the condition number κ=L/μ\kappa = L/\mu. Smaller κ\kappa (well-conditioned problems) means faster convergence; this is the theoretical seed for why Momentum Methods and adaptive preconditioning matter, since they effectively reduce the effective condition number the algorithm experiences.

Honest tradeoffs.

  • The O(1/T)O(1/T) and linear rates require knowing or bounding LL (or μ\mu); in practice η\eta is tuned empirically or found via line search.
  • Convexity is essential to correctness: on non-convex losses (e.g., deep networks), gradient descent still decreases ff under the same smoothness argument, but it can only be shown to converge to a stationary point (∥∇f∥→0\|\nabla f\| \to 0), which may be a saddle point or local minimum, not the global one.
  • Batch gradient descent's per-step cost of O(mn)O(mn) makes it unattractive for large mm — this is precisely the gap Stochastic Gradient Descent closes at the cost of noisier updates and a slower, sublinear-in-expectation convergence guarantee even under strong convexity.

Worked example

Take the simple convex quadratic f(x)=12m−1∑i=1m(x−ai)2f(x) = \frac{1}{2} m^{-1}\sum_{i=1}^m (x - a_i)^2 — batch gradient descent on the mean-squared-error objective for estimating a single scalar parameter xx from data. Let the (tiny) training set be a={1,3,8}a = \{1, 3, 8\}, so m=3m = 3. The objective is f(x)=16[(x−1)2+(x−3)2+(x−8)2]f(x) = \frac{1}{6}\left[(x-1)^2 + (x-3)^2 + (x-8)^2\right], with

∇f(x)=f′(x)=13[(x−1)+(x−3)+(x−8)]=13(3x−12)=x−4.\nabla f(x) = f'(x) = \frac{1}{3}\left[(x-1) + (x-3) + (x-8)\right] = \frac{1}{3}(3x - 12) = x - 4.

This is LL-smooth with L=1L = 1 (the second derivative is constant at 1) and strongly convex with μ=1\mu = 1, so κ=1\kappa = 1 — a perfectly conditioned 1-D problem. The true minimum is at x∗=4x^* = 4 (the mean of the data), with f∗=f(4)=16(9+1+16)=266≈4.333f^* = f(4) = \frac{1}{6}(9 + 1 + 16) = \frac{26}{6} \approx 4.333. Using η=1/L=1\eta = 1/L = 1 and starting at x0=10x_0 = 10:

ttxtx_t∇f(xt)=xt−4\nabla f(x_t) = x_t - 4xt+1=xt−η∇f(xt)x_{t+1} = x_t - \eta \nabla f(x_t)f(xt)f(x_t)
010.0006.0004.00022.333
14.0000.0004.0004.333 (= f∗f^*)

With the theoretically optimal step size for this well-conditioned quadratic, gradient descent reaches the exact minimum in a single step — consistent with the linear-convergence bound, since κ=1\kappa = 1 implies (1−μ/L)T=0(1 - \mu/L)^T = 0 for T≥1T \ge 1. Now watch what happens with a poorly chosen learning rate, η=2.5\eta = 2.5 (exceeding 2/L=22/L = 2, the classical stability threshold for quadratics):

ttxtx_t∇f(xt)\nabla f(x_t)xt+1x_{t+1}f(xt)f(x_t)
010.006.0010−2.5(6)=−5.0010 - 2.5(6) = -5.0022.33
1-5.00-9.00−5−2.5(−9)=17.50-5 - 2.5(-9) = 17.5033.33
217.5013.5017.5−2.5(13.5)=−16.2517.5 - 2.5(13.5) = -16.25106.08

The iterate oscillates with growing amplitude and diverges — f(xt)f(x_t) is increasing, violating the descent property, exactly because η>2/L\eta > 2/L breaks the smoothness-based inequality used in the proof. This trace demonstrates concretely why the choice of η\eta relative to LL is not a minor tuning detail but the entire hinge on which the correctness proof depends.

Common pitfalls & traps

PitfallWhy it happensFix
Learning rate too largeViolates η≤1/L\eta \le 1/L (or 2/L2/L for quadratics); descent lemma no longer holdsUse η≤1/L\eta \le 1/L; estimate LL or use backtracking line search
Learning rate too smallConverges, but O(1/T)O(1/T) rate becomes practically glacialTune η\eta, or use adaptive/decaying schedules (see Learning Rates and Convergence)
Treating stationary points as global minima on non-convex ffZero-gradient condition only implies global optimality under convexityRecognize the convexity assumption; use multiple restarts or specialized non-convex theory for deep models
Forgetting the 1/m1/m normalization or averaging incorrectlySums instead of averages inflate the effective learning rate by a factor of mmExplicitly divide by mm when constructing the batch gradient
Using batch gradient descent naively on massive datasetsO(mn)O(mn) per step becomes a bottleneck at web scaleSwitch to mini-batch or Stochastic Gradient Descent
Ignoring ill-conditioning (κ≫1\kappa \gg 1)Convergence rate (1−μ/L)T(1-\mu/L)^T becomes very close to 1, so progress per step is tinyPrecondition, rescale features, or use momentum/second-order methods

Pioneers & history

Gradient-based iterative minimization traces back to 19th-century numerical analysis, long before "machine learning" existed as a field, and was later absorbed wholesale into optimization theory and then into learning algorithms.

Origins · 1 minAugustin-Louis Cauchy and how this was discovered
AC
Augustin-Louis Cauchy
1789–1857 · French mathematician who, in an 1847 note on solving simultaneous equations, first described the method of steepest descent for iteratively minimizing a function using its derivatives

HC
Haskell Curry
1900–1982 · American mathematician and logician who in 1944 gave one of the first rigorous convergence analyses of the steepest-descent method for general nonlinear functions

History
1847
— Cauchy introduces the method of steepest descent for systems of equations.
1944
— Curry provides a formal convergence analysis of gradient-based descent methods.
1951
— Robbins and Monro introduce stochastic approximation, the theoretical root of Stochastic Gradient Descent.
1986
— Backpropagation combined with gradient descent popularized for training multilayer neural networks (Rumelhart, Hinton, Williams).
2010
s — Adaptive and momentum-accelerated variants (AdaGrad, Adam, Nesterov momentum) become standard in deep learning pipelines.

Where this matters

Gradient descent and its variants form the optimization backbone of essentially every trainable model in modern machine learning. Linear and logistic regression, support vector machines with smooth surrogate losses, and generalized linear models are commonly fit with batch or mini-batch gradient descent when closed-form solutions are unavailable or too costly for high-dimensional data. Every major deep learning framework — PyTorch, TensorFlow, JAX — implements gradient descent as the base case from which stochastic and adaptive optimizers (SGD with momentum, Adam, RMSProp) are built, all sharing the same core update rule analyzed here. Recommendation systems, computer vision pipelines, and large language model pretraining all reduce, at the lowest level, to variants of this single iterative rule applied at massive scale with mini-batches replacing full-batch gradients. Convex optimization solvers used in operations research, control theory, and statistics (e.g., ridge regression, Lasso via proximal gradient methods) also rely on the exact convergence theory developed for convex smooth objectives here, extended with proximal or projected variants for constrained and non-smooth cases.

Ask the tutor: How does adding momentum change the convergence guarantees of gradient descent on ill-conditioned convex problems?

Why does gradient descent move in the negative gradient direction rather than the gradient direction itself?

The gradient ∇f(x)\nabla f(x) points in the direction of steepest local increase of ff, by definition of the directional derivative being maximized along that direction. Since the goal is minimization, moving opposite to this direction — the negative gradient — locally decreases ff fastest among all unit directions. This follows from a first-order Taylor expansion: f(x−η∇f(x))≈f(x)−η∥∇f(x)∥2f(x - \eta \nabla f(x)) \approx f(x) - \eta \|\nabla f(x)\|^2, which is strictly smaller than f(x)f(x) for any sufficiently small η>0\eta > 0 whenever the gradient is nonzero. Moving in the positive gradient direction would instead be gradient ascent, used for maximizing functions such as log-likelihoods in some formulations.

Does gradient descent always find the global minimum?

Only under specific conditions — primarily convexity. For convex differentiable functions, any point with zero gradient is provably a global minimizer, so gradient descent's convergence to a stationary point is equivalent to convergence to the global optimum. For non-convex functions, such as most deep neural network losses, gradient descent is only guaranteed to converge to a stationary point, which could be a local minimum or a saddle point. In practice, over-parameterized deep networks tend to have many equally good local minima, and empirical evidence suggests saddle points are more of an obstacle than bad local minima, but this remains an active area distinct from the clean convex theory covered here.

How is the learning rate η chosen in practice if the smoothness constant L is unknown?

In practice, LL is rarely known exactly, especially for complex models, so practitioners rely on empirical tuning, learning rate schedules, or line search methods. A backtracking line search starts with a guess for η\eta and shrinks it until the descent condition (loss actually decreases sufficiently) is satisfied at each step, adapting automatically without needing LL in closed form. Alternatively, cross-validation over a grid of candidate learning rates on a held-out validation set is common in applied machine learning. The topic of principled learning rate selection and adaptive schedules is developed further in Learning Rates and Convergence.

What is the difference between batch gradient descent and stochastic gradient descent?

Batch gradient descent computes the exact gradient of the full objective using every training example before taking a single step, giving accurate but computationally expensive updates. Stochastic gradient descent instead estimates the gradient using one example (or a small mini-batch) per step, producing a noisy but computationally cheap approximation that still points in roughly the right direction on average. This tradeoff means SGD can take many more steps in the time batch gradient descent takes one, often reaching a good solution faster in wall-clock time despite each individual step being less accurate. The full analysis of this tradeoff, including its own convergence theory, belongs to Stochastic Gradient Descent.

Why is the convergence rate O(1/T) for convex functions but linear (geometric) for strongly convex functions?

Strong convexity adds a curvature lower bound absent in plain convexity: the function must curve upward at least as fast as a quadratic with coefficient μ>0\mu > 0 in every direction. This extra structure lets the proof bound the distance to the optimum itself, ∥xt−x∗∥\|x_t - x^*\|, shrinking by a constant multiplicative factor (1−μ/L)(1 - \mu/L) every iteration — compounding exponentially over TT steps. Plain convexity only bounds the function value gap, and without a curvature floor, the best guaranteed rate is the weaker sublinear O(1/T)O(1/T). This is exactly why the condition number κ=L/μ\kappa = L/\mu is the central quantity determining practical convergence speed.

Can gradient descent get stuck oscillating even without technically diverging?

Yes — when η\eta is close to but under the stability threshold 2/L2/L for smooth quadratics, iterates can zig-zag across narrow valleys in the loss surface, especially when the Hessian is ill-conditioned (large κ=L/μ\kappa = L/\mu). This produces very slow effective progress even though each step technically satisfies the descent property. This exact pathology, caused by mismatched curvature across different directions in parameter space, is the primary motivation for Momentum Methods, which add a velocity term to smooth out this oscillation and accelerate convergence along consistent descent directions.

Is gradient descent used directly in modern deep learning, or only its variants?

Pure batch gradient descent, as analyzed here, is rarely used directly for large-scale deep learning because full-dataset gradient computation per step is prohibitively expensive at modern data scales. Instead, mini-batch stochastic gradient descent and adaptive optimizers like Adam, which combine stochastic gradients with momentum and per-parameter learning rate adaptation, are the practical default. However, the core update rule — move against an estimate of the gradient, scaled by a learning rate — and the theoretical machinery (smoothness, convexity, descent lemmas) developed for batch gradient descent remain the conceptual foundation that all these variants build upon and are analyzed against.

Before this

Where this leads