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.
- Gradient descent minimizes a differentiable function by repeatedly stepping in the direction of the negative gradient.
- The update rule is , where is the learning rate.
- On convex, -smooth objectives with a suitable fixed step size, gradient descent converges to the global minimum.
- Convergence rate is 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.
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 .
The idea
Consider a differentiable function that we want to minimize — in machine learning, is typically an empirical loss function of model parameters averaged over a training set. The gradient is the vector of partial derivatives, and it points in the direction of steepest increase of at . It follows immediately that points in the direction of steepest decrease, which is the entire justification for the algorithm's core move.
Gradient descent is the iterative scheme
where is the learning rate (or step size), and indexes iterations starting from some initial guess . In the specific case called batch gradient descent, when is an average loss over training examples, the gradient is computed using all 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 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 ) and, optionally, strong convexity (a lower-curvature bound ). 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 and in evaluating 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 over data pairs , 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 suffices for convergence guarantees on smooth convex functions, but many implementations decay over time (e.g., ) to squeeze out faster practical convergence, foreshadowing the deeper analysis in Learning Rates and Convergence. 2. Convergence check. In practice, stopping is triggered by , by a small relative change in , 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.
Complexity & correctness
Per-iteration cost. For batch gradient descent on training examples with parameters, computing the full gradient costs time in the typical case where each per-example gradient costs (e.g., linear models) — more generally where is the cost of one example's gradient. Space is for storing and , plus 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 . This is where convexity and smoothness earn their keep. Assume:
- is convex and differentiable.
- is -smooth: for all (the gradient doesn't change too fast).
- A global minimizer exists with .
Claim (convex, smooth case). With fixed step size , gradient descent satisfies
Proof sketch. -smoothness gives the quadratic upper bound (the descent lemma):
Substituting and using :
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 , which after algebraic manipulation with the previous bound produces a telescoping sum:
Since is non-increasing (from the descent property), the minimum term is at most the average of the sum, giving the stated rate.
Claim (strongly convex case). If additionally is -strongly convex (), then with ,
a linear (geometric) convergence rate — exponentially fast in , governed by the condition number . Smaller (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 and linear rates require knowing or bounding (or ); in practice 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 under the same smoothness argument, but it can only be shown to converge to a stationary point (), which may be a saddle point or local minimum, not the global one.
- Batch gradient descent's per-step cost of makes it unattractive for large — 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 — batch gradient descent on the mean-squared-error objective for estimating a single scalar parameter from data. Let the (tiny) training set be , so . The objective is , with
This is -smooth with (the second derivative is constant at 1) and strongly convex with , so — a perfectly conditioned 1-D problem. The true minimum is at (the mean of the data), with . Using and starting at :
| 0 | 10.000 | 6.000 | 4.000 | 22.333 |
| 1 | 4.000 | 0.000 | 4.000 | 4.333 (= ) |
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 implies for . Now watch what happens with a poorly chosen learning rate, (exceeding , the classical stability threshold for quadratics):
| 0 | 10.00 | 6.00 | 22.33 | |
| 1 | -5.00 | -9.00 | 33.33 | |
| 2 | 17.50 | 13.50 | 106.08 |
The iterate oscillates with growing amplitude and diverges — is increasing, violating the descent property, exactly because breaks the smoothness-based inequality used in the proof. This trace demonstrates concretely why the choice of relative to is not a minor tuning detail but the entire hinge on which the correctness proof depends.
Common pitfalls & traps
| Pitfall | Why it happens | Fix |
|---|---|---|
| Learning rate too large | Violates (or for quadratics); descent lemma no longer holds | Use ; estimate or use backtracking line search |
| Learning rate too small | Converges, but rate becomes practically glacial | Tune , or use adaptive/decaying schedules (see Learning Rates and Convergence) |
| Treating stationary points as global minima on non-convex | Zero-gradient condition only implies global optimality under convexity | Recognize the convexity assumption; use multiple restarts or specialized non-convex theory for deep models |
| Forgetting the normalization or averaging incorrectly | Sums instead of averages inflate the effective learning rate by a factor of | Explicitly divide by when constructing the batch gradient |
| Using batch gradient descent naively on massive datasets | per step becomes a bottleneck at web scale | Switch to mini-batch or Stochastic Gradient Descent |
| Ignoring ill-conditioning () | Convergence rate becomes very close to 1, so progress per step is tiny | Precondition, 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
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 points in the direction of steepest local increase of , 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 fastest among all unit directions. This follows from a first-order Taylor expansion: , which is strictly smaller than for any sufficiently small 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, 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 and shrinks it until the descent condition (loss actually decreases sufficiently) is satisfied at each step, adapting automatically without needing 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 in every direction. This extra structure lets the proof bound the distance to the optimum itself, , shrinking by a constant multiplicative factor every iteration — compounding exponentially over steps. Plain convexity only bounds the function value gap, and without a curvature floor, the best guaranteed rate is the weaker sublinear . This is exactly why the condition number is the central quantity determining practical convergence speed.
Can gradient descent get stuck oscillating even without technically diverging?
Yes — when is close to but under the stability threshold for smooth quadratics, iterates can zig-zag across narrow valleys in the loss surface, especially when the Hessian is ill-conditioned (large ). 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.