Partial derivatives
f(x, y) = x² + 2y² takes two inputs and gives one number. Picture it as a height above a map. Contour rings join points of equal height, like a hiking map. The lowest point is (0, 0), where f = 0.
Stand at x = 2, y = 1. The height there is f = 2² + 2 · 1² = 4 + 2 = 6. The pin marks the point, and its stem is as tall as f.
Freeze y and change only x. Then f is x² plus a constant, so its slope is the partial derivative ∂f/∂x = 2x. At x = 2 that is 4: stepping in x, the height rises about 4 per unit.
Now freeze x and change only y. The 2y² term gives ∂f/∂y = 4y, which is 4 at y = 1. Each partial derivative is an ordinary slope along one axis, with the other input held still.
The gradient
Put the partial derivatives side by side: ∇f = (∂f/∂x, ∂f/∂y) = (4, 4), read "grad f". It is a vector, an arrow on the map. Add the two arrows tip to tail and you get it.
The gradient points where f rises fastest, at right angles to the contour ring through you. Its length, √(4² + 4²) ≈ 5.66, is that fastest rate. Walking along the ring, f stays at 6: the slope there is 0.
Gradient descent
To lower f, step against the gradient: new point = old − lr · ∇f. The learning rate lr sets the step size. With lr = 0.25: (2, 1) − 0.25 · (4, 4) = (1, 0), and f drops from 6 to 1.
At (1, 0) the gradient is (2, 0). Step again: (1, 0) − 0.25 · (2, 0) = (0.5, 0), and f = 0.25. Each step here halves x, so the pin slides toward the lowest point, (0, 0).
The learning rate matters. Start again at (2, 1) with lr = 0.5. Now x lands on 0, but y overshoots the valley: (0, −1), then (0, 1), then (0, −1) again. f stays at 2 and never reaches the bottom.
Training a model works the same way. f is the loss, a score of the model's errors, and the inputs are its weights, often millions of them. Libraries compute the gradient for you (backpropagation) and repeat this update many times.
Try it in the Python prompt (one session)
;; step 1/10 · A height for every point >>> def f(x, y): ... return x**2 + 2*y**2 ... >>> f(0, 0), f(1, 0), f(0, 1) (0, 1, 2)
The arrows on the map are drawn at a quarter of their true length (0.25, the learning rate), so the descent arrow lands exactly on the next point. ∂ is a curly d. ∂f/∂x means "the derivative of f in x, with every other input held still". ∇ is called nabla or del.