Calculus for Learning
Partial derivatives and gradients
The gradient: which way is uphill
A one-input function has one slope. But a loss function has millions of inputs, every weight in a model, and at any point it slopes differently in every direction. The gradient is the elegant trick that tames this.
Stand on a hillside. In every direction you could face, the ground tilts by a different amount, steep behind you, flat to your left. A is the slope when you look along one fixed compass line. The bundles all those slopes together and reports the single direction that climbs fastest, along with how steep that climb is. To go down the hill, which is all a model ever wants, turn around and step the opposite way.
Partial derivatives: wiggle one input, freeze the rest
Take a function of two inputs, , a perfectly round bowl sitting above the plane. To find its slope we borrow the one-variable derivative and add a rule: vary a single input and treat every other input as a constant. The result is a partial derivative, written with a curly instead of .
At the point the -slope is : push a hair and the height climbs six times as fast. Push instead and it climbs only . Same spot, two very different slopes.
The gradient: all the partials, stacked into one arrow
Nothing forces us to pick a direction. Stack the partials into a vector and you get the gradient, written (say "grad f" or "del f").
At the gradient is , an arrow pointing mostly east and a little north. Its direction is where the hill rises fastest; its length is how steep that fastest climb is. Why does that one arrow capture the steepest direction? Because the slope along any unit direction is just a dot product with the gradient, the directional derivative.
Perpendicular to the contour
Contour maps, and descending the negative gradient
A contour map draws the loops where holds one fixed value. Rings packed tightly mean a steep slope; rings spread apart mean gentle ground. The gradient stabs straight across the rings, uphill. And here is the entire idea of learning, distilled: to shrink the loss, step in the negative gradient direction, , the steepest way down. Click the map, press Descend or Run, then drag the learning rate up and watch a calm descent turn into overshoot, oscillation, and finally divergence.
Ready - press Descend for one step, or Run to animate.
The stretched valley is where alpha earns its keep
The update rule, in vector form
Gradient descent in two dimensions, in nine lines.
import numpy as np
def grad(p): # grad f for f = x^2 + y^2 -> [2x, 2y]
return np.array([2 * p[0], 2 * p[1]])
p = np.array([4.0, 4.0]) # start somewhere on the map
alpha = 0.1 # the learning rate (step size)
for step in range(50):
p = p - alpha * grad(p) # step DOWN the negative gradient
print(p) # -> near [0, 0], the bottom of the bowlCheck yourself
A partial derivative df/dx measures the slope when you:
Relative to the contour line you are standing on, the gradient points:
What does the gradient point toward, and how do we use it to learn?
Direction and length both carry meaning. Try to state it, then check.
Lock it in
- A partial derivative varies one input and freezes the rest.
- The gradient stacks the partials; it points uphill, steepest first.
- The gradient is perpendicular to the contour you stand on.
- Learning is p := p - alpha·grad f, one rule at any number of dimensions.
Primary source