Gradients
About this lesson
A function of two inputs is a landscape: every point on a map has a height. Follow one point, (2, 1), on the bowl f(x, y) = x² + 2y². Measure its slope along x and along y, stack the two partial derivatives into the gradient vector (4, 4), see why it points the steepest way uphill, and then step against it, again and again, the way gradient descent trains machine-learning models.
A height for every point
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.
>>> def f(x, y): ... return x**2 + 2*y**2 ... >>> f(0, 0), f(1, 0), f(0, 1) (0, 1, 2)
Read the lesson step by step
A height for every point
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.
>>> def f(x, y): ... return x**2 + 2*y**2 ... >>> f(0, 0), f(1, 0), f(0, 1) (0, 1, 2)Stand at one point
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.
>>> f(2, 1) 6Partial derivative in x
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.
>>> h = 1e-6 >>> round((f(2 + h, 1) - f(2, 1)) / h, 3) 4.0Partial derivative in y
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.
>>> round((f(2, 1 + h) - f(2, 1)) / h, 3) 4.0The gradient vector
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.
>>> def grad(x, y): ... return 2*x, 4*y ... >>> grad(2, 1) (4, 4)The steepest way up
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.
>>> import math >>> gx, gy = grad(2, 1) >>> round(math.hypot(gx, gy), 2) 5.66 >>> (gx*1 + gy*-1) / math.sqrt(2) 0.0Step downhill
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.
>>> x, y, lr = 2, 1, 0.25 >>> gx, gy = grad(x, y) >>> x, y = x - lr*gx, y - lr*gy >>> (x, y), f(x, y) ((1.0, 0.0), 1.0)Repeat the step
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).
>>> gx, gy = grad(x, y) >>> (gx, gy) (2.0, 0.0) >>> x, y = x - lr*gx, y - lr*gy >>> (x, y), f(x, y) ((0.5, 0.0), 0.25)Too big a step
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.
>>> x, y, lr = 2, 1, 0.5 >>> for _ in range(3): ... gx, gy = grad(x, y) ... x, y = x - lr*gx, y - lr*gy ... print((x, y), f(x, y)) ... (0.0, -1.0) 2.0 (0.0, 1.0) 2.0 (0.0, -1.0) 2.0Gradients in machine learning
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.
>>> x, y, lr = 2, 1, 0.25 >>> for _ in range(20): ... gx, gy = grad(x, y) ... x, y = x - lr*gx, y - lr*gy ... >>> x, y (1.9073486328125e-06, 0.0)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01For f(x, y) = x² + 2y², what is ∂f/∂y at the point (3, 2)?
Hold x still: the x² term is a constant, and 2y² has slope 4y. At y = 2 that is 4 · 2 = 8. The value of x does not matter here.
02What is the gradient of f(x, y) = x² + 2y² at (1, 1)?
∇f = (∂f/∂x, ∂f/∂y) = (2x, 4y). At (1, 1) that is (2, 4).
03At a point where the gradient is not zero, which way does the gradient point?
The slope in a unit direction u is ∇f · u, which is largest when u points along ∇f and zero along the contour. It does not always aim at the minimum: on this bowl, at (2, 1) it points along (4, 4), not back toward (0, 0).
04You are at (2, 1) on f(x, y) = x² + 2y² with learning rate lr = 0.25. Where does one gradient descent step take you?
∇f(2, 1) = (4, 4). New point = (2, 1) − 0.25 · (4, 4) = (2 − 1, 1 − 1) = (1, 0), where f drops from 6 to 1.
05On the same f, why does lr = 0.5 never reach the bottom from (2, 1)?
The y update is y − lr · 4y = (1 − 4·lr) · y. For lr = 0.5 the factor is −1, so y bounces between 1 and −1 and f stays at 2. y settles only when 0 < lr < 0.5.
Glossary
- Function of two variables
- A rule that takes two inputs, such as x and y, and gives one number, such as f(x, y) = x² + 2y². You can picture it as a height above a map.
- Contour line (level curve)
- All the points where f has the same value, like a line of equal height on a hiking map.
- Partial derivative
- The slope of f along one input while every other input is held still. ∂f/∂x is the slope in the x direction.
- Gradient (∇f)
- The vector of all the partial derivatives, (∂f/∂x, ∂f/∂y). It points the way f rises fastest, and its length is that fastest rate.
- Directional derivative
- The slope of f in a chosen direction u (a vector of length 1). It equals ∇f · u.
- Gradient descent
- Repeatedly stepping against the gradient, new point = old point − lr · ∇f, to make f smaller.
- Learning rate (lr)
- The number that scales each gradient descent step. Too small is slow; too large overshoots or diverges.
- Loss function
- In machine learning, the f being minimised: a score of how wrong the model's predictions are, as a function of its weights.
Notes and sources
- 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.
- The slope in any direction u (a vector of length 1) is the directional derivative ∇f · u. It is largest, |∇f|, when u points along ∇f, and 0 along a contour ring.
- For this f, y changes by the factor 1 − 4·lr each step, so y settles only when 0 < lr < 0.5. At lr = 0.5 it flips sign forever; above 0.5 it grows.
- ∂ is a curly d. ∂f/∂x means "the derivative of f in x, with every other input held still". ∇ is called nabla or del.
- Python prints -1.0; the figure writes −1. Both are the same number.
- MIT OCW 18.02SC Multivariable Calculus: Partial derivatives, gradient and directional derivatives
- G. Strang, Calculus (MIT OCW), chapter 13: Partial derivatives and gradients
- Goodfellow, Bengio and Courville, Deep Learning, section 4.3: Gradient-Based Optimization
- Goodfellow, Bengio and Courville, Deep Learning, section 6.5: Back-Propagation
- Stanford CS231n notes: Optimization and gradient descent
- Python library: math.hypot
Quiz Unlocks after the last step.