Gradient Descent: Intuition, Choosing the Step Size, and Worked Examples in R

Why the step size decides whether gradient descent converges, and three ways to choose it in R: the exact step, Armijo-Wolfe conditions, and bisection.

Sylla N'falyAug 09, 20245 min readTranslated from French

Imagine you are lost in the middle of a mountain range, blindfolded, and your only goal is to reach the lowest valley. Your instinct would tell you to feel the ground, sense the slope, and take a step in the direction where the descent is steepest.

That is the principle of gradient descent, the optimization algorithm at the heart of machine learning. The picture has an important limit, though: it tells you which direction to walk, not how long each step should be. And that choice decides whether you reach the bottom in a few steps, in a few thousand, or never.

The problem: one step size for every direction

The whole article follows the same example, an elongated valley:

The minimum is at , but the slope is seven times steeper along than along . With a fixed step , each iteration multiplies by and only by :

Fixed step of 0.1 from (5, 5): y collapses, x crawlsAuthor's figure
Elliptical contour lines of f; the path drops almost vertically to the x-axis, then moves toward the origin in small, regular steps.

In three steps we go from to : the steep direction is settled, while the other one advances by 10% per step. Increasing the step to move faster along does not work: beyond , the factor exceeds 1 and diverges. The right step depends on where we are, hence the idea of recomputing it at every iteration.

Three ways to choose the step

01 / Exact step

For a quadratic function, we compute the step that minimizes f along the descent direction.

02 / Armijo and Wolfe

Two conditions that tell whether a step is "good enough": neither too long nor too short.

03 / Bisection

A simple way to find a step that satisfies both conditions, by cutting the interval in half.

Exact step for a quadratic function

Let be the value of f after a step of length . For a quadratic , is a parabola in , and its minimum can be computed directly:

Here and , so:

f          <- function(x, y) x^2/2 + 7*y^2/2
grad_f     <- function(x, y) c(x, 7*y)
step_exact <- function(x, y) (x^2 + 49*y^2) / (x^2 + 343*y^2)
 
gd_exact <- function(x0, y0, iters = 200, tol = 1e-10) {
  x <- x0; y <- y0
  for (i in 1:iters) {
    g <- grad_f(x, y)
    if (sqrt(sum(g^2)) < tol) break   # zero gradient: minimum reached
    a <- step_exact(x, y)
    x <- x - a*g[1]; y <- y - a*g[2]
  }
  c(x = x, y = y)
}

From , 23 iterations are enough to bring the gradient norm below . The method is ideal when it applies, but it assumes we know : for an arbitrary function, there is no formula.

The Armijo and Wolfe conditions

Without a formula, we look for an acceptable step rather than an optimal one. Two conditions are enough: if f is bounded below and its gradient is Lipschitz continuous, any step that satisfies them drives the gradient to zero (Nocedal and Wright, chapter 3, Zoutendijk's theorem):

  1. Armijo condition (sufficient decrease)

    The step must lower f by at least a fraction of what the initial slope promises. It rejects steps that are too long, those that cross the bottom of the valley and climb up the other side:

  2. Curvature condition (Wolfe)

    Armijo alone accepts tiny steps: they always lower f a little. The curvature condition rejects them by requiring that, after the step, the descent has become clearly less steep:

    If the step is too short, the gradient has barely changed: the product stays close to , the condition fails, and the step must be lengthened.

Steps accepted by Armijo and Wolfe from (5, 5)
U-shaped curve of φ(α). A green band covers the accepted steps, from about 0.015 to 0.29; on the left, the steps that are too short; on the right, those where φ rises above the Armijo line. The exact step, 0.145, sits at the bottom of the curve.

In our example, with and , the accepted steps range from about 0.015 to 0.29. The exact step (0.145) is one of them, but any step in the band guarantees sufficient progress. These two values are the ones Nocedal and Wright recommend for Newton-type methods.

Finding an acceptable step by bisection

The two conditions bracket the right step: if Armijo fails, the step is too long; if the curvature condition fails, it is too short. We start from an interval and halve it until we land in the band:

line_search_bisection <- function(x, y, g, c1 = 1e-4, c2 = 0.9, a_max = 1, max_iter = 50) {
  gg <- sum(g^2)
  armijo_ok   <- function(a) f(x - a*g[1], y - a*g[2]) <= f(x, y) - c1*a*gg
  courbure_ok <- function(a) sum(grad_f(x - a*g[1], y - a*g[2]) * g) <= c2*gg
  lo <- 0; hi <- a_max; a <- (lo + hi)/2
  for (j in 1:max_iter) {
    if (!armijo_ok(a)) hi <- a          # too long: keep the left half
    else if (!courbure_ok(a)) lo <- a   # too short: keep the right half
    else break                          # both conditions hold
    a <- (lo + hi)/2
  }
  a
}
 
gd_wolfe <- function(x0, y0, iters = 200, tol = 1e-10) {
  x <- x0; y <- y0
  for (i in 1:iters) {
    g <- grad_f(x, y)
    if (sqrt(sum(g^2)) < tol) break
    a <- line_search_bisection(x, y, g)
    x <- x - a*g[1]; y <- y - a*g[2]
  }
  c(x = x, y = y)
}

From , gd_wolfe converges to in 93 iterations, and every search finds a valid step. It is slower than the exact step (23 iterations): that is the price of generality, since this version assumes nothing about the shape of f, only that we can compute f and its gradient.

A second case: Armijo backtracking alone

The most common version in practice is even simpler: start from and halve it until Armijo is satisfied. On the well-conditioned quadratic , whose minimum is at , that is enough:

f_q    <- function(x, y) x^2 + y^2 + x*y - x - 2*y
grad_q <- function(x, y) c(2*x + y - 1, 2*y + x - 2)
 
pas_armijo <- function(x, y, c1 = 1e-4, shrink = 0.5) {
  g <- grad_q(x, y); a <- 1
  while (f_q(x - a*g[1], y - a*g[2]) > f_q(x, y) - c1*a*sum(g^2)) a <- a*shrink
  c(x - a*g[1], y - a*g[2])
}
Armijo backtracking from (−5, 5)Author's figure
Tilted elliptical contour lines; the path starts at (−5, 5), jumps to (1, 2), then converges in a few steps to the minimum (0, 1).

The first full step () is accepted and leads straight to ; the following ones oscillate around the minimum, closing in. When the function is well conditioned, nothing more is needed.

What to monitor

The code in this article was checked numerically (iterations, end point, accepted steps) through a line-by-line transcription; the two trajectory figures come from the original version of the article.

References

  1. Nocedal, J., & Wright, S. J. (2006). Numerical Optimization (2nd ed.), chapter 3: Line Search Methods. Springer.
  2. Wolfe conditions, Wikipedia: statement, strong variant and usual values of c₁ and c₂.
All posts