Math for AILoading progress…

Gradients as directions

Perform gradient descent updates and explain learning rate, stochastic gradients, momentum and Adam.

Many inputs, one slope per input

A loss L depends on parameters. With two parameters, L(x, y) is a landscape: the height above the point (x, y). The partial derivative ∂L/∂x is the slope you meet if you walk along x and keep y fixed. ∂L/∂y is the same for y. The gradient simply stacks them, always in the same order as the parameters: ∇L = [∂L/∂x, ∂L/∂y]. For L = x² + y² the partials are 2x and 2y, so at (1, 2) the gradient is [2, 4].

Contour plots draw a landscape from above: every line connects points of equal loss, like the height lines on a hiking map. Close lines mean steep ground.

Why the gradient is the steepest direction

Pick any direction as a unit vector u. The slope you feel walking that way is the dot product g · u. A dot product is largest when u points the same way as g, and then it equals the length |g|. So the gradient points in the direction of fastest increase, its length is that fastest slope, and −g / |g| is the direction of fastest decrease. Walking along a contour line, perpendicular to g, changes the loss by zero (to first order).

L = x² + y² point = [3, -2] L = 13 gradient = [6, -4] |g| = 7.211 steepest descent = [-0.832, 0.555]
u = [1, 0] slope along u = g · u = 6·1 + -4·0 = 6 largest possible slope = |g| = 7.211

Click the plot (or focus it and use the arrow keys) to move the point. Red: the gradient, the direction of steepest increase. Green: its opposite, the direction of steepest decrease. Purple dashes: the direction you choose with the slider.

Round contours. Both coordinates have the same curvature, so the steepest direction points straight at the minimum.

Click anywhere on the plot (or use the arrow keys after focusing it). Red is the gradient, green is the steepest descent direction. Turn the purple direction slider and watch g · u: it peaks when purple lies on red, and it is zero along the contour.

Predict first. For L = x² + y², what is the gradient at (3, −2)?

Steepest does not mean toward the minimum

Switch the plot to Elongated valley and click at [4, 0.5]. The gradient is [0.16, 1]: nearly all of it is the y part, because the valley wall is steep across and almost flat along. The steepest descent direction therefore points almost straight at the valley wall opposite, about 74 degrees away from the direction to the minimum. The gradient is local information: it says which way is downhill right here, not where the bottom is.

The same lesson holds at flat spots. At a point where the gradient is exactly zero you cannot tell by the gradient alone whether you are at a minimum. On the saddle surface (L = x² - y² + 0.25 y⁴) the origin has gradient [0, 0], yet the loss is 0 there while it is −1 at (0, ±√2).

Common mistakes

Mixing up the order of partials. An update subtracts the first entry from the first parameter. If you hand back [∂L/∂y, ∂L/∂x], each parameter moves by the other one's slope:

L = x**2 + 3*y**2 + x*y          # at (1, 2)
correct gradient = [2*x + y, 6*y + x] = [4, 13]
swapped gradient = [13, 4]
# x would be pushed by y's slope: the step no longer follows the landscape
Chapter context

The gradient points toward the steepest local increase under Euclidean geometry. To lower a differentiable loss, a basic optimizer steps in the opposite direction. A local direction says little about how large a safe step can be.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

θt+1=θt−η∇L(θt).\theta_{t+1}=\theta_t-\eta\nabla L(\theta_t).

Adam uses mt=β1mt−1+(1−β1)gtm_t=\beta_1m_{t-1}+(1-\beta_1)g_t and vt=β2vt−1+(1−β2)gt2v_t=\beta_2v_{t-1}+(1-\beta_2)g_t^2, coordinatewise. Divide by 1−β1t1-\beta_1^t and 1−β2t1-\beta_2^t before using m^t/(v^t+ϵ)\hat m_t/(\sqrt{\hat v_t}+\epsilon) in the update.

For the special quadratic L(θ)=(θ−c)2L(\theta)=(\theta-c)^2, the residual follows θt+1−c=(1−2η)(θt−c)\theta_{t+1}-c=(1-2\eta)(\theta_t-c). Starting away from c, convergence requires ∣1−2η∣<1|1-2\eta|<1, or 0<η<10<\eta<1. This interval depends on this particular curvature and loss normalization.

Worked example

For L=(theta−3)^2, start at 0 and use eta=0.25. The gradients are −6, −3 and −1.5. Three updates produce theta values 1.5, 2.25 and 2.625. Their losses are 2.25, 0.5625 and 0.140625. A fixed learning rate works here because the example is a simple quadratic.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
theta = 0.0
positions = []
for _ in range(3):
    theta -= 0.25 * 2*(theta-3)
    positions.append(theta)
assert positions == [1.5, 2.25, 2.625]
assert isclose((theta-3)**2, 0.140625)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Subtracting loss instead of its gradient is not gradient descent. Squared gradients in Adam are coordinatewise. A zero gradient may identify a saddle point or maximum, not just a minimum.