Gradients
The gradient: collecting all partial derivatives into one vector that points in the direction of steepest increase.
The previous article computed partial derivatives one at a time: the rate of change along one axis, with everything else frozen. But a function of variables has such derivatives. Treating them as separate numbers misses the bigger picture. Collected into a vector, they form the gradient, which encodes how the function changes in any direction at once.
Definition
The gradient of a scalar function is the column vector of all its partial derivatives, evaluated at a point :
The symbol is called nabla, and is read "grad f." The gradient lives in the same space as the input: a function on has a gradient in .
Two examples computed from the previous article's rules:
At : . At the origin: .
At : , matching the partial derivatives from the previous article.
Direction of Steepest Ascent
The gradient is more than a convenient grouping of numbers. It carries a precise geometric meaning: points in the direction of steepest increase of at .
To see why, consider the rate of change of along any unit vector , called the directional derivative:
Using the dot product formula , and since :
where is the angle between and . This is largest when , i.e., when points in the same direction as . The gradient is therefore exactly the direction of fastest increase.
Three consequences:
- gives the direction of steepest ascent: increases as fast as possible along it.
- gives the direction of steepest descent: decreases as fast as possible along it.
- marks a critical point: no direction is locally uphill or downhill.
Level Curves and Perpendicularity
A level curve (or level set) is the set of all points where takes a fixed value :
For , the level curves are circles centered at the origin. For a general surface, they are the "contour lines" of a topographic map.
The gradient at any point is perpendicular to the level curve through that point. Moving along a level curve keeps constant, so the directional derivative in that direction is zero:
Since the dot product with a tangent direction is zero, the gradient must be orthogonal to it. The result is clean: to increase , you need to cross the level curves, not follow them, and the gradient shows you exactly how.
Magnitude
The magnitude tells you how steep the function is at :
- Large : the function changes rapidly in all directions near .
- Small : the surface is nearly flat near .
- : a critical point, possibly a minimum, a maximum, or a saddle.
The maximum rate of change in any direction is exactly , achieved only along itself.
Local Linear Approximation
The gradient provides the best linear approximation to near a point. For a small displacement :
This is the multi-variable first-order Taylor expansion. It says: a small step changes by approximately the dot product of the gradient with that step. The error shrinks as .
This formula will reappear many times: it is the foundation of how small weight changes propagate through a network.
Connection to Deep Learning
In deep learning, the quantities we care about are functions that map many numbers to a single scalar. A model's prediction quality, for instance, depends on all of its parameters at once.
For such a function , the gradient is a vector with one entry per parameter. Each entry describes how sensitive the scalar output is to a small change in :
- Large positive: increasing strongly increases the output.
- Near zero: barely affects the output at the current point.
- Large negative: increasing strongly decreases the output.
In a modern model, can be in the billions. The gradient is an -dimensional vector that carries all of this sensitivity information in one object. What to do with it — how to act on it to improve the model — is the subject of later articles.
In Code
Computing the gradient numerically extends the finite-difference approach from the previous article. Perturb each coordinate in turn, holding all others fixed:
def numerical_grad(f, x, eps=1e-5):
g = []
for i in range(len(x)):
xp = x[:i] + [x[i] + eps] + x[i+1:]
xm = x[:i] + [x[i] - eps] + x[i+1:]
g.append((f(xp) - f(xm)) / (2 * eps))
return g
# f(x, y) = x² + y², so ∇f = [2x, 2y]
f = lambda x: x[0]**2 + x[1]**2
print(numerical_grad(f, [1.0, 2.0])) # [2.0, 4.0]
print(numerical_grad(f, [3.0, 0.0])) # [6.0, 0.0]
# g(x, y) = x² + 3xy, so ∇g = [2x + 3y, 3x]
g = lambda x: x[0]**2 + 3*x[0]*x[1]
print(numerical_grad(g, [2.0, 1.0])) # [7.0, 6.0]
The gradient tells us everything about local change. For a function of a handful of variables, computing it by finite differences is perfectly adequate. For a deep network with millions of parameters, this approach would require millions of forward passes, one per weight. The chain rule makes it possible to compute the exact gradient in a single backward pass, and that is where we go next.