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, , so that a single Vector describes the slope in every direction at once.
The symbol , 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 of inputs, the gradient at a point is
Each entry is the partial derivative with respect to one input. The gradient is evaluated at a particular point, so 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 (length 1), the rate of change of in direction is the directional derivative
The right side uses the dot product. By the geometry of the dot product, , where is the angle between and . This is largest when , which means points the same way as the gradient, and then the rate of change equals the length . The opposite direction, , 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 takes one fixed value, such as a contour line on a map. Moving along a level set does not change , 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 smooth function is well approximated by a plane:
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 , which is most negative when is the negative gradient direction.
Gradient descent
The update rule that uses this fact is
where is the learning rate, a number that sets the step size. For a small enough , each step lowers . Too large an overshoots, and the function can grow instead. Choosing is a practical problem in its own right.
Worked examples
Let . Find at .
- Partial with respect to , holding fixed: .
- Partial with respect to , holding fixed: .
- So .
- At : .
Interpretation: moving in the direction increases at rate 8 and in the direction at rate 3. The steepest direction is itself, and the steepest rate is .
Let , a round bowl, and stand at .
- Gradient: , so at it is .
- Length: . This is the steepest slope.
- Unit direction of steepest ascent: .
- Check by directional derivative along : , less than 10, as expected.
- Check along the steepest direction: . Matches the length.
The gradient points straight away from the centre of the bowl, which is what steepest ascent on a bowl should do.
Use the bowl from the last example, start at where , and take a learning rate .
- Gradient at the start: .
- Step: .
- New value: .
The function dropped from 25 to 16 in one step. Each step scales the point by toward the centre, so shrinks by per step. That is the exact behaviour for this bowl, and it shows the gradient step doing its job.
- slope of the bowl in direction at angle θ
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.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Learning rate too large | Steps overshoot, and the loss oscillates or grows | Reduce , or use a schedule that decays it |
| Learning rate too small | Progress is painfully slow | Increase , or use an adaptive method such as Adam |
| Elongated valley (very different curvature per axis) | Steps zigzag across the valley and crawl along it | Preconditioning, momentum, or a second-order method |
| Flat plateau or saddle point | The gradient is near zero, so the update stalls | Momentum, or added noise to escape the saddle |
| Non-smooth function (a kink) | The gradient does not exist at the kink | Subgradients, or smooth the function |
| Gradient taken over a huge dataset | Each step is expensive | Stochastic 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 (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 , what is at , and what is the steepest rate of increase there?Show answer
The partials are and . At they are and , so . The steepest rate is its length, .
QuestionWhich direction gives zero rate of change for at a point where , and why?Show answer
Any direction perpendicular to , such as or , gives zero rate of change. The rate in direction is , and this is zero exactly when is perpendicular to the gradient. Those directions run along the level set through the point.
QuestionFor at , take one gradient descent step with . What is the new point and new value?Show answer
The gradient is , so the step is . The new value is . This lands exactly on the minimum. Any here overshoots past it (at the point jumps to , no better than the start), and makes 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 , or use a schedule that decreases it over training. The gradient itself is not the problem.
Sources
- Gradient (Wikipedia): https://en.wikipedia.org/wiki/Gradient
- Gradient descent (Wikipedia): https://en.wikipedia.org/wiki/Gradient_descent
- Multivariable calculus (Wikipedia): https://en.wikipedia.org/wiki/Multivariable_calculus