Skip to content

CalculusLesson 3 of 6

Use slopes to find minima

Stationary points and gradient descent

The prediction f(x+Δx)f(x)+f(x)Δxf(x + \Delta x) \approx f(x) + f'(x)\,\Delta x describes the change for a small step to the right:

  • f(x)>0f'(x) > 0: ff goes up. Rising.
  • f(x)<0f'(x) < 0: ff goes down. Falling.
  • f(x)=0f'(x) = 0: nothing happens, to first order. Flat.

The size says how fast. A slope of 6 turns a step of 0.010.01 into a rise of about 0.060.06.

Suppose ff has a smooth peak at xx, and suppose the slope there were positive. Then a tiny step right would go higher. So it wasn’t a peak. Suppose the slope were negative. A tiny step left would go higher. Same problem. At an interior differentiable peak, the slope must be zero. The same reasoning applies to an interior valley.

For a differentiable function, an interior maximum or minimum must satisfy f(x)=0f'(x)=0. These points are candidates: you must also check boundaries and any points where the derivative does not exist. For example, find the lowest point of f(x)=x24x+1f(x)=x^2-4x+1.

f(x)=2x4=0x=2,f(2)=3f'(x) = 2x - 4 = 0 \quad\Longrightarrow\quad x = 2, \qquad f(2) = -3

Completing the square proves the global minimum: f(x)=(x2)233f(x)=(x-2)^2-3\ge-3, with equality at x=2x=2. Checking two nearby points alone would not prove a minimum.

A zero derivative does not guarantee a maximum or minimum: x3x^3 has derivative 0 at the origin but keeps increasing through it. A local minimum also need not be the lowest point across the whole domain.

If ff' is differentiable, its derivative ff'', “f double prime”, measures how quickly the slope changes. If ff is position, ff' is velocity and ff'' is acceleration.

At a flat spot, ff'' tells hill from valley:

  • f>0f'' > 0: the slope is increasing, from negative through zero to positive. Falling, then rising. A valley.
  • f<0f'' < 0: rising, then falling. A hilltop.
  • f=0f'' = 0: no verdict.

For x24x+1x^2 - 4x + 1, f=2>0f'' = 2 > 0 everywhere. One bowl, and x=2x = 2 is its bottom.

f(x) = x⁴/4 − x²: two valleys and a hilltop. Drag the point to read f′ and f″, then step downhill.

Solving 2x4=02x-4=0 gives the minimum directly. For a neural network, solving all the corresponding equations is usually impractical. We can instead use the current derivative to choose a direction: a positive slope points downhill to the left, and a negative slope points downhill to the right.

xnew=xηf(x)x_{\text{new}} = x - \eta\, f'(x)

η\eta (“eta”) is the learning rate: it scales the derivative to determine the step. The approximation f(xηf)f(x)η(f)2f(x-\eta f')\approx f(x)-\eta(f')^2 predicts a decrease when the derivative is nonzero and the step is sufficiently small. Repeating this can approach a local minimum; the result depends on the function, starting point, and learning rate. Here are the first steps from x=5x=5 on x24x+1x^2-4x+1, using η=0.1\eta=0.1:

stepxxf(x)=2x4f'(x) = 2x - 4next xx
0564.4
14.44.83.92
23.923.843.536
33.5363.0723.229

Heading to 2, in shrinking steps. This is gradient descent in one dimension.

Go deeper: how big a step?

A downhill direction does not make every step downhill. On x24x+1x^2-4x+1 from x=5x=5, η=1\eta=1 lands at x=1x=-1, at the same height as the starting point; η=1.1\eta=1.1 lands higher. For a quadratic with constant curvature f=c>0f^{\prime\prime}=c>0, fixed steps converge to its minimum when 0<η<2/c0<\eta<2/c. Other functions need their own checks: curvature may change along the step. This is why the learning rate matters.

A position function gives velocity when differentiated. The next lesson uses a rate over an interval to calculate an accumulated change.

Definition

Read the full glossary entry →