Gradient Descent

Iterative first-order optimisation algorithm for differentiable loss functions.

beginner classical ml optimisationcalculus
optimisationcalculuslearning-rateconvergence

Authors @Praneeth-Suresh , @N00bcak Updated 2026-07-20

Overview

In the most general sense, training a model usually means choosing parameters that make a loss function small. In Linear Regression, for example, we choose weights and a bias to minimise the mean-squared error between predictions and labels.

There are, of course, many ways to minimise said loss, as exemplified by Linear Regression:

How do we do Gradient Descent?

For Gradient Descent, the name of the game is to start with arbitrary parameters and iteratively improve them by moving opposite to the gradient of our loss function (thus, roughly speaking, the loss function is assumed differentiable with respect to the parameters).

An Analogy for Gradient Descent

We can intuitively understand this process by likening it to hikers descending a mountain in fog so thick they can only see a few steps ahead.

So the story goes:

The hikers cannot see the bottom of the mountain, so they can only rely on their immediate surroundings to feel their way down. They can feel the slope of the ground, or measure it with instruments, and determine the locally steepest downhill direction. They then discuss how far to move before stopping to measure the slope again. As the fog is too thick, they cannot know whether they have reached the lowest point of the mountain. They might instead stop in a local valley, make very slow progress across a nearly flat region, or repeatedly overshoot a narrow valley by taking steps that are too large.

This analogy captures a few key recurring themes of Gradient Descent that are worth highlighting:

Gradient DescentHiker Analogy
Global optimum is unknownHikers cannot see the bottom of the mountain
Slope at current point is knownHikers can feel or measure how the ground slopes where they are standing
Loss function is differentiableHikers can feel/measure the slope of the ground
Best-effort updates based on local informationHikers move in their perceived direction of steepest descent
Step size is chosen by the learning rateHikers discuss how far to move before measuring again
Convergence to global optima not guaranteedHikers may never reach the bottom of the mountain

and it should also reveal that Gradient Descent is actually a pretty crass algorithm (after all, real-life hikers would be quite cooked if they actually encountered such a scenario).

Yet, gradient descent is one of the most widely used optimisation algorithms in machine learning. We will explore how it works, and why this is the case in the next few sections.

Algorithm

To go into further details, let’s assume we are dealing with a regression task, and write some (extremely sloppy) math down:

In Gradient Descent (for regression tasks):

Worked Example

Consider a toy loss L(f(x;θ),y)=12(θ−y)2\mathcal{L}(f(\mathbf{x}; \theta), \mathbf{y}) = \frac{1}{2}(\theta - \mathbf{y})^2 where θ,x,y∈R\theta, \mathbf{x}, \mathbf{y} \in \mathbb{R} and f(x;θ)=θf(\mathbf{x}; \theta) = \theta.

With θ=0\theta = 0, y=3\mathbf{y} = 3, and η=0.5\eta = 0.5, the update rule becomes

θt+1=θt−0.5(θt−3)∀t≥0\theta_{t+1}=\theta_t-0.5(\theta_t-3) \quad \forall t\ge 0

You can trace out the first few updates and check against the table below.

Iteration ttCurrent θt\theta_tGradient ∇L(θt)\nabla\mathcal{L}(\theta_t)Update −η∇L(θt)-\eta\nabla\mathcal{L}(\theta_t)Next θt+1\theta_{t+1}
00-31.51.5
11.5-1.50.752.25
22.25-0.750.3752.625

Baby’s First Training Dynamics Analysis

Mechanically tracing how updates evolve can familiarize us with the procedure of gradient descent, but it is arguably more useful to study how the updates change as training goes on (“training dynamics”).

Continuing with our toy example, we can observe from the above that every update moves θt\theta_t (ever more slowly) towards 3. In fact, rearranging gives

θt+1−3=(1−η)(θt−3),\theta_{t+1}-3=(1-\eta)(\theta_t-3),

so (at least when we have a nice loss like this), the multiplier 1−η1-\eta completely determines HOW the weights evolve, as summarized below:

Learning rateBehaviour on this example
0<η<10 \lt \eta \lt 1Approaches 3 from the same side; very small values make slow progress.
η=1\eta=1Reaches 3 in one step.
1<η<21 \lt \eta \lt 2Oscillates back and forth around 3, but eventually settles down.
η=2\eta=2Oscillates around 3 without getting closer.
η>2\eta \gt 2Overcorrects and diverges to ±∞\pm\infty.

This shows something treated as quite obvious by ML practitioners: that learning rate is a hyperparameter that must be carefully tuned.

Convergence, Convexity, and Polyak-Lojasiewicz

Typically, we do not run gradient descent indefinitely. We stop when we are satisfied with the solution, and we then call the model “converged” (onto a set of parameters θ†\theta^\dag).

What “satisfied” means varies quite wildly depending on who you ask:

However, if we are willing to put up some strong assumptions on the loss function and assume it is convex, we can guarantee every local minimum must be a global minimum.

Unfortunately, many loss functions are non-convex, so most practitioners default to the last option, and use convergence as a vague conceptual tool to guide their training process instead.

Advanced: Links to Proofs of Convergence

Note: All three results below assume the objective is LL-smooth on the region being analysed, meaning its gradient is LL-Lipschitz continuous:

∥∇L(θ1)−∇L(θ2)∥≤L∥θ1−θ2∥for all θ1,θ2 in that region.\lVert\nabla\mathcal{L}(\theta_1)-\nabla\mathcal{L}(\theta_2)\rVert \leq L\lVert\theta_1-\theta_2\rVert \quad \text{for all } \theta_1,\theta_2 \text{ in that region.}

For their strongly convex result, Boyd and Vandenberghe assume mI⪯∇2L(θ)⪯MImI \preceq \nabla^2\mathcal{L}(\theta) \preceq MI on the initial sublevel set, so MM plays the role of the smoothness constant. Karimi, Nutini, and Schmidt state the PL result using an LL-Lipschitz continuous gradient.

Convex Functions

  • Without assuming strong convexity or PL, Theorem L12.6 in MIT’s gradient descent notes proves that gradient descent on a convex, LL-smooth objective with a minimizer has a sublinear objective-gap rate of O(1/t)O(1/t) when 0<η≤1/L0 \lt \eta \leq 1/L.

Strongly Convex Functions

  • Boyd and Vandenberghe prove in section 9.3.1 that the objective gap under gradient descent converges linearly when the objective is strongly convex and its Hessian is bounded above, provided the learning rate is chosen appropriately. Strong convexity is stronger than ordinary convexity and guarantees a unique global minimizer.

Polyak-Lojasiewicz Functions

  • A differentiable objective satisfies the Polyak-Lojasiewicz (PL) condition if there is some μ>0\mu \gt 0 such that 12∥∇L(θ)∥2≥μ(L(θ)−L∗)\frac{1}{2}\lVert\nabla\mathcal{L}(\theta)\rVert^2 \geq \mu(\mathcal{L}(\theta)-\mathcal{L}^*). Strong convexity implies the PL condition, but the PL condition does not require convexity. For an objective that is both PL and smooth and has a minimizer, Karimi, Nutini, and Schmidt show that gradient descent with an appropriate learning rate makes the objective gap converge linearly to zero.

Batch, Stochastic, and Mini-Batch Gradients

Up to this point, we’ve discussed gradient descent (GD) as if we had access to the true gradient of the loss function at every step.

In this regime, we typically break up the loss function into an aggregate of per-example losses (such as “average”), then compute the gradient of this aggregate loss at every step. This is called batch GD:

L(f(x;θ),y)=1n∑i=1∣D∣ℓ(f(xi;θ),yi)∇θL=1n∑i=1∣D∣∇θℓ(f(xi;θ),yi)\mathcal{L}(f(\mathbf{x}; \theta), \mathbf{y}) = \frac{1}{n}\sum_{i=1}^{|\mathcal{D}|} \ell(f(\mathbf{x}_i; \theta), \mathbf{y}_i) \quad \quad \nabla_\theta \mathcal{L} = \frac{1}{n}\sum_{i=1}^{|\mathcal{D}|} \nabla_\theta \ell(f(\mathbf{x}_i; \theta), \mathbf{y}_i)

This is rarely the case in practice, and with finite (or even small) datasets (relative to a presumed infinite population), we are forced to work with noisy estimates of our loss (and therefore, its gradient) at every step. We often express the gradient as

∇θL=1n∑i=1n∇θℓ(f(xi;θ),yi)+ϵi(μ;σ2)\nabla_\theta \mathcal{L} = \frac{1}{n}\sum_{i=1}^n \nabla_\theta \ell(f(\mathbf{x}_i; \theta), \mathbf{y}_i) + \epsilon_i(\mu; \sigma^2)

where ϵi\epsilon_i is a random variable with mean μ\mu and variance σ2\sigma^2 (and, if the batch size is big enough, ϵ=1n∑i=1nϵi\epsilon = \frac{1}{n}\sum_{i=1}^n \epsilon_i is approximately Gaussian by the Central Limit Theorem).

Using a single, truly-randomly selected sample (i.e. n = 1) gives us stochastic GD (SGD, not to be confused with the optimizer of the same name), while using a small, truly-randomly selected subset of samples (i.e. n<∣D∣n < |\mathcal{D}|) gives us mini-batch GD.

In the vaguest of senses, here are some considerations when using these three variants of GD (which essentially vary nn):

Linear Regression provides a concrete convex loss on which the update and convergence ideas are easiest to see. In Neural Networks, backpropagation computes the gradient and a gradient-based optimiser applies the parameter update. Transformers inherit this training loop from neural networks, usually with mini-batches and an extension of plain gradient descent.

Learning Path

What should I learn before Gradient Descent?

Start with linear-regression before studying Gradient Descent.

How does Gradient Descent connect to other AI topics?

Gradient Descent belongs to Classical ML and connects to optimisation, calculus, optimisation, calculus, learning-rate, convergence.

Further Reading

  1. Convex Optimization – Boyd & Vandenberghe
  2. Optimization Methods for Large-Scale Machine Learning – Bottou, Curtis & Nocedal
  3. A Stochastic Approximation Method – Robbins & Monro
  4. Some Methods of Speeding Up the Convergence of Iteration Methods – Polyak
  5. Gradient Descent and Descent Lemmas – MIT 6.7220/15.084