1def grad(x, y):2 return 2*x, 4*y3x, y, lr = 2, 1, 0.254gx, gy = grad(x, y)5x -= lr*gx; y -= lr*gyxyf(x,y)∂f/∂x∂f/∂ylrxy(0, 0)12469
Height map: contour rings of f, the lowest point at (0, 0)Pin: its stem is as tall as f at the pointArrows: ∂f/∂x, ∂f/∂y, the gradient ∇f and the step −lr·∇fCells and Python card: the numbers and the code of one stepA height map of f(x, y) = x² + 2y²: contour rings join points of equal height, and the pin's stem is as tall as f where it stands. Red and blue arrows are the partial derivatives ∂f/∂x and ∂f/∂y; added tip to tail they make the gradient ∇f (yellow), which crosses the ring at a right angle. The green arrow is the descent step −lr · ∇f, and the dashed trail shows where the pin has been. 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.

Partial derivatives

  1. 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.

The gradient

Gradient descent

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.