Partial derivatives and the gradient
With many inputs, nudge one at a time: that's a partial derivative. Stack them all and you get the gradient, an arrow pointing straight uphill.
1. The idea
Most functions in machine learning take many inputs: a loss depends on every weight. A partial derivative, ∂f/∂x, asks the one-variable question about one input at a time. Hold every other input fixed, nudge this one, and see how fast f changes.
Collect all the partial derivatives into a vector and you have the gradient, ∇f. It points in the direction in which f increases fastest, its length says how steep that is, and it always crosses the contour lines (where f is constant) at right angles. The negative gradient is the steepest way down.
That is why training works. The loss is a function of millions of weights; backpropagation computes ∇L; and every step moves the weights a little way along −∇L.
2. Try it: follow the arrows downhill
Drag the point P across the contour lines. The bright arrow is ∇f at P; the faint ones are the gradient elsewhere. Press the button a few times to walk downhill.
3. The math
4. The code (python)
import numpy as np
def f(p):
x, y = p
return x**2 + x * y + 2 * y**2
def grad(p):
x, y = p
return np.array([2 * x + y, x + 4 * y])
p = np.array([1.5, 1.0])
print(grad(p)) # [4. 5.5]
h = 1e-5 # check each partial by nudging one input at a time
numeric = [(f(p + h * e) - f(p - h * e)) / (2 * h) for e in np.eye(2)]
print(np.round(numeric, 4)) # [4. 5.5]
u = np.array([1.0, -1.0]) / np.sqrt(2) # the slope in a chosen direction
print(round(grad(p) @ u, 4)) # -1.06075. You’ll see this in
- Core Course chapter 7: the training loop
- Gradient descent, read with the seven questions
- Module 1: the dot product why the gradient is the steepest direction
6. Check yourself
Finished this chapter?
What changed in ML architectures this week
New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.