Computable Secrets / companion article

Gradient Descent

Still from the video Gradient DescentWatch ad-free in the member player

or watch free on YouTube ↗

You have a function. You want to find where it is smallest. That is the entire problem.

Walk downhill#

Start with one variable. If the derivative is positive, the function is increasing, so move left. If the derivative is negative, the function is decreasing, so move right. Always walk downhill.

That gives a rule: take your current position and subtract the derivative, scaled by a small positive number α,

xn+1=xn-αf(xn).

That is one step of gradient descent. Watch it run on a curve and each step moves the point closer to the bottom.

The learning rate#

α is called the learning rate, and it is a tradeoff. Set it too large and you overshoot the minimum, bouncing back and forth across it. Set it too small and you inch forward, wasting computation. The right α depends on the problem.

Pause and try: take f(x)=x2, start at x0=1, and run two steps by hand with α=0.1. Then try α=1.

The derivative is f(x)=2x, so the update is xn+1=xn-0.2xn=0.8xn. Two steps: x1=0.8, x2=0.64. Every step multiplies the distance to the minimum by 0.8, steady convergence.

With α=1 the update is xn+1=xn-2xn=-xn. From x0=1 you get 1,-1,1,-1, forever: the overshoot exactly mirrors you to the other side of the valley, and you never descend at all. Same function, same rule, wrong α.

Millions of variables#

One variable is a toy. In practice you minimize functions of thousands or millions of variables, and the derivative becomes the gradient, the vector of all partial derivatives. The key geometric fact: the gradient at a point is the direction of steepest ascent. To minimize, go the opposite way. Negative gradient, steepest descent.

The update rule keeps the same shape. Take your current parameter vector, subtract α times the gradient, repeat:

θn+1=θn-αf(θn).

On a contour plot, each step cuts across the level curves toward the center.

The catch#

Gradient descent follows the local slope, so it finds local minima. If the function has many valleys, you might land in a shallow one and miss the deepest.

Despite this, gradient descent is the engine behind modern machine learning. Neural networks have loss functions with millions of parameters and countless local minima, and yet gradient descent consistently finds parameters that generalize well. The theory of why is still an active area of research, though in high dimensions the dominant obstacles appear to be saddle points and plateaus, not bad local minima.

One rule, walk downhill. Applied at scale, it learns to recognize faces, translate languages, and fold proteins.

If you remember three things#

  1. Gradient descent is one rule applied repeatedly: θn+1=θn-αf(θn), a step against the gradient, which is the direction of steepest ascent.
  2. The learning rate α is a tradeoff: too large overshoots and oscillates, too small crawls, and the right value depends on the problem.
  3. It only promises local minima, yet at the scale of modern machine learning it reliably finds parameters that generalize, and explaining why is still open research.

Discussion

No comments yet. Be the first.