ENع
←Gradients
Lesson 23 · Linear algebra and calculus

Gradients

14 min10 steps5 questionsAdvanced
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 23 · Interactive
01 / 10

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)
Loading figure…
or ← →
Read the lesson step by step
  1. 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)
  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)
    6
  3. Partial 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.0
  4. Partial 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.0
  5. The 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)
  6. 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.0
  7. Step 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)
  8. 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)
  9. 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.0
  10. Gradients 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)

Quiz Unlocks after the last step.