Search

Search papers and lessons

Gradient

Level 2 · 12 min read

The gradient collects a function's partial derivatives into one vector that points in the direction the function increases fastest.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

TL;DR

The gradient of a function with several inputs is the vector of its partial derivatives, one entry per input. It answers two questions at once: how fast the function changes in each input direction, and which way to move to make it grow fastest. Moving a small step against the gradient makes the function shrink fastest, which is the basis of almost all model training.

Where you'll see it: in gradient descent, which trains neural networks by stepping each weight against its partial derivative; in backpropagation, which computes these partials layer by layer; and in physics, where a potential's gradient gives the force.

Intuition

A hill with a compass

Stand on a hillside at some spot. The ground rises in some directions and falls in others. Ask a single question: if I take one step, which way should I face to climb as steeply as possible? The answer is a direction, and a number for how steep it is. That direction and steepness, bundled together, is the gradient.

Partial derivatives answer a narrower question. Each one measures the slope in one axis direction, with the other inputs frozen. Two axes means two partials, one east-west and one north-south. The gradient just stacks them into one object, ∇f=(∂f/∂x,∂f/∂y)\nabla f = (\partial f / \partial x, \partial f / \partial y), so that a single Vector describes the slope in every direction at once.

The symbol ∇\nabla, read "nabla" or "del", is the upside-down Greek delta. It is a shorthand for "take all the partials and put them in a list".

flowchart LR
  F[Function f of many inputs] --> P[Each partial derivative: slope along one axis]
  P --> G[Stack them: the gradient vector]
  G --> D[Points uphill, steepest]
  G --> N[Its negative points downhill, steepest]

Why a vector and not just a number

A single number cannot say which way is uphill. Two inputs with the same slope size can climb in opposite directions. Only a vector keeps both the steepness and the orientation. Its length is the steepest slope, and its direction is the way to go.

The math

Definition

For a function f(x1,…,xn)f(x_1, \dots, x_n) of nn inputs, the gradient at a point is

∇f=(∂f∂x1, ∂f∂x2, …, ∂f∂xn).\nabla f = \left( \frac{\partial f}{\partial x_1},\ \frac{\partial f}{\partial x_2},\ \dots,\ \frac{\partial f}{\partial x_n} \right).

Each entry is the partial derivative with respect to one input. The gradient is evaluated at a particular point, so ∇f\nabla f is a different vector at each location. It is a function from points to vectors, sometimes called a vector field.

Direction of steepest ascent

For a unit vector u\mathbf{u} (length 1), the rate of change of ff in direction u\mathbf{u} is the directional derivative

Duf=∇f⋅u.D_{\mathbf{u}} f = \nabla f \cdot \mathbf{u}.

The right side uses the dot product. By the geometry of the dot product, ∇f⋅u=∥∇f∥cos⁡θ\nabla f \cdot \mathbf{u} = \lVert \nabla f \rVert \cos\theta, where θ\theta is the angle between ∇f\nabla f and u\mathbf{u}. This is largest when θ=0\theta = 0, which means u\mathbf{u} points the same way as the gradient, and then the rate of change equals the length ∥∇f∥\lVert \nabla f \rVert. The opposite direction, θ=π\theta = \pi, gives the most negative rate of change.

So the gradient points in the direction of steepest ascent, and its length is the steepest slope.

Level sets are perpendicular to the gradient

A level set is the set of points where ff takes one fixed value, such as a contour line on a map. Moving along a level set does not change ff, so the rate of change along it is zero. The direction of zero change is perpendicular to the gradient, so the gradient is always at right angles to the contour lines. This is why, on a contour map, steep slopes are where the lines sit close together.

Linearisation

Near a point a\mathbf{a}, a smooth function is well approximated by a plane:

f(a+h)≈f(a)+∇f(a)⋅h.f(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a}) \cdot \mathbf{h}.

This is the multivariable version of the tangent line. It explains why a small step against the gradient lowers the function: the change is approximately ∇f⋅h\nabla f \cdot \mathbf{h}, which is most negative when h\mathbf{h} is the negative gradient direction.

Gradient descent

The update rule that uses this fact is

xt+1=xt−η ∇f(xt),\mathbf{x}_{t+1} = \mathbf{x}_t - \eta\, \nabla f(\mathbf{x}_t),

where η>0\eta \gt 0 is the learning rate, a number that sets the step size. For a small enough η\eta, each step lowers ff. Too large an η\eta overshoots, and the function can grow instead. Choosing η\eta is a practical problem in its own right.

Worked examples

Example
Computing a gradient

Let f(x,y)=x2+3xyf(x, y) = x^2 + 3xy. Find ∇f\nabla f at (1,2)(1, 2).

  1. Partial with respect to xx, holding yy fixed: ∂f/∂x=2x+3y\partial f / \partial x = 2x + 3y.
  2. Partial with respect to yy, holding xx fixed: ∂f/∂y=3x\partial f / \partial y = 3x.
  3. So ∇f=(2x+3y, 3x)\nabla f = (2x + 3y,\ 3x).
  4. At (1,2)(1, 2): ∇f=(2+6, 3)=(8,3)\nabla f = (2 + 6,\ 3) = (8, 3).

Interpretation: moving in the xx direction increases ff at rate 8 and in the yy direction at rate 3. The steepest direction is (8,3)(8, 3) itself, and the steepest rate is 64+9≈8.54\sqrt{64 + 9} \approx 8.54.

Example
Steepest direction on a bowl

Let f(x,y)=x2+y2f(x, y) = x^2 + y^2, a round bowl, and stand at (3,4)(3, 4).

  1. Gradient: ∇f=(2x,2y)\nabla f = (2x, 2y), so at (3,4)(3, 4) it is (6,8)(6, 8).
  2. Length: ∥∇f∥=36+64=10\lVert \nabla f \rVert = \sqrt{36 + 64} = 10. This is the steepest slope.
  3. Unit direction of steepest ascent: (6,8)/10=(0.6,0.8)(6, 8)/10 = (0.6, 0.8).
  4. Check by directional derivative along (1,0)(1, 0): (6,8)⋅(1,0)=6(6, 8) \cdot (1, 0) = 6, less than 10, as expected.
  5. Check along the steepest direction: (6,8)⋅(0.6,0.8)=3.6+6.4=10(6, 8) \cdot (0.6, 0.8) = 3.6 + 6.4 = 10. Matches the length.

The gradient (6,8)(6, 8) points straight away from the centre of the bowl, which is what steepest ascent on a bowl should do.

Example
One step of gradient descent

Use the bowl f(x,y)=x2+y2f(x, y) = x^2 + y^2 from the last example, start at (3,4)(3, 4) where f=25f = 25, and take a learning rate η=0.1\eta = 0.1.

  1. Gradient at the start: (6,8)(6, 8).
  2. Step: x1=(3,4)−0.1×(6,8)=(3−0.6, 4−0.8)=(2.4,3.2)\mathbf{x}_1 = (3, 4) - 0.1 \times (6, 8) = (3 - 0.6,\ 4 - 0.8) = (2.4, 3.2).
  3. New value: f(2.4,3.2)=5.76+10.24=16f(2.4, 3.2) = 5.76 + 10.24 = 16.

The function dropped from 25 to 16 in one step. Each step scales the point by 0.80.8 toward the centre, so ff shrinks by 0.82=0.640.8^2 = 0.64 per step. That is the exact behaviour for this bowl, and it shows the gradient step doing its job.

0123456−10−50510direction angle θ (radians)rate of change
  • slope of the bowl in direction at angle θ
For the bowl at (3, 4), the rate of change in a direction at angle θ is the dot product of the gradient with that unit direction. It peaks at 10 at θ ≈ 0.927, the gradient's own direction.

Tradeoffs and failure modes

The gradient is local. It describes the slope at one point, and the step it suggests is trustworthy only for small distances. Large steps use the wrong slope and can overshoot.

SituationWhat goes wrongBetter choice
Learning rate too largeSteps overshoot, and the loss oscillates or growsReduce η\eta, or use a schedule that decays it
Learning rate too smallProgress is painfully slowIncrease η\eta, or use an adaptive method such as Adam
Elongated valley (very different curvature per axis)Steps zigzag across the valley and crawl along itPreconditioning, momentum, or a second-order method
Flat plateau or saddle pointThe gradient is near zero, so the update stallsMomentum, or added noise to escape the saddle
Non-smooth function (a kink)The gradient does not exist at the kinkSubgradients, or smooth the function
Gradient taken over a huge datasetEach step is expensiveStochastic gradient estimates from mini-batches

A common mistake is to treat the gradient as a global map of the function. It is a first-order, local statement. Over long distances, curvature matters, and the gradient alone cannot see it.

History

Partial derivatives were developed in the 18th century by Euler, Lagrange and others working on functions of several variables, and vector analysis took its modern shape in the late 19th century. Hamilton introduced the symbol ∇\nabla (a rotated delta) in his work on quaternions, and the name "nabla", after a harp-like instrument of similar shape, came later from his followers, notably Peter Guthrie Tait.

Gradient descent is older than machine learning: Cauchy described the method of steepest descent in 1847 for solving systems of equations. Its place in modern training came with scale. A network has millions of weights, and the backpropagation algorithm, popularised in the 1980s by Rumelhart, Hinton and Williams, computes the gradient of one loss with respect to all of them by applying the chain rule backwards. Gradient descent plus backpropagation became the standard training recipe.

Check yourself

QuestionFor f(x,y)=x2yf(x, y) = x^2 y, what is ∇f\nabla f at (2,1)(2, 1), and what is the steepest rate of increase there?Show answer

The partials are ∂f/∂x=2xy\partial f / \partial x = 2xy and ∂f/∂y=x2\partial f / \partial y = x^2. At (2,1)(2, 1) they are 44 and 44, so ∇f=(4,4)\nabla f = (4, 4). The steepest rate is its length, 16+16=42≈5.66\sqrt{16 + 16} = 4\sqrt{2} \approx 5.66.

QuestionWhich direction gives zero rate of change for ff at a point where ∇f=(3,4)\nabla f = (3, 4), and why?Show answer

Any direction perpendicular to (3,4)(3, 4), such as (4,−3)(4, -3) or (−4,3)(-4, 3), gives zero rate of change. The rate in direction u\mathbf{u} is ∇f⋅u\nabla f \cdot \mathbf{u}, and this is zero exactly when u\mathbf{u} is perpendicular to the gradient. Those directions run along the level set through the point.

QuestionFor f(x,y)=x2+y2f(x, y) = x^2 + y^2 at (3,4)(3, 4), take one gradient descent step with η=0.5\eta = 0.5. What is the new point and new value?Show answer

The gradient is (6,8)(6, 8), so the step is (3,4)−0.5(6,8)=(0,0)(3, 4) - 0.5(6, 8) = (0, 0). The new value is f(0,0)=0f(0, 0) = 0. This η\eta lands exactly on the minimum. Any η>0.5\eta \gt 0.5 here overshoots past it (at η=1\eta = 1 the point jumps to (−3,−4)(-3, -4), no better than the start), and η>1\eta \gt 1 makes ff grow.

QuestionA training run shows the loss oscillating up and down with growing amplitude. The gradient is correct. What is the most likely cause, and what would you change?Show answer

The learning rate is too large, so each step overshoots the minimum by more than it gains. Reduce η\eta, or use a schedule that decreases it over training. The gradient itself is not the problem.

Sources