Skip to main content

Gradient Descent in One Variable

Gradient descent iteratively seeks a minimum. To maximize ff, minimize −f-f instead. This approach is foundational in optimization problems where exact solutions are challenging to derive analytically, especially due to complexities in higher dimensions.

Concept of Gradient Descent​

  • Initially demonstrated in a single-variable context to ease into the more complex multi-variable gradient descent.
  • Employs an iterative process to approximate the minimum of a function by systematically updating the point of interest based on the function's derivative.

Mathematical Formulation​

Given a function f(x)=ex−log⁡(x)f(x) = e^x - \log(x), finding its minimum involves:

  1. Derivative Calculation: The first step is to compute the derivative f′(x)=ex−1xf'(x) = e^x - \frac{1}{x}.
  2. Iterative Update: Starting from an initial point x0x_0, the next point x1x_{1} is determined by x1=x0−α⋅f′(x0)x_{1} = x_{0} - \alpha \cdot f'(x_{0}), where α\alpha is the learning rate.

This method leverages the derivative to guide the direction of steps taken towards the minimum. The sign of the derivative indicates whether to move left or right (in the case of a single variable).

Challenges and Solutions​

Analytical Difficulty​

  • Directly solving ex=1xe^x = \frac{1}{x} for xx is analytically challenging, exemplifying situations where gradient descent offers a practical solution.

Learning Rate (α\alpha)​

  • The learning rate α\alpha is critical in ensuring the iterative steps are appropriately sized to prevent overshooting or excessively slow convergence.
  • Adaptive Learning Rates: Research into adaptive learning rates seeks to dynamically adjust α\alpha based on the optimization progress, though a universally optimal strategy is yet to be established.
Gradient-descent iterates on a quadratic with a small learning rate.Open full-size image

Here f(x) = x², the start is x = 10 and the learning rate is 0.05. Orange points show ten updates: they move toward zero but only reach about 3.49. This separate quadratic example illustrates small steps; the function earlier on this page is eˣ − log(x).

Gradient-descent iterates on a quadratic with a large learning rate.Open full-size image

With the same function and start but a learning rate of 1.1, the iterates cross zero and grow in magnitude. The update is x ← −1.2x, so the distance increases by 20% each time. Compare axis scales: this plot covers much larger values than the one above.

Local Minima​

  • Gradient descent may converge to local minima, potentially missing the global minimum.
  • Multiple Initial Points: Employing multiple starting points and running gradient descent iterations from each can enhance the likelihood of approaching the global minimum.

Practical Implementation​

  1. Initialization: Choose a starting point and learning rate.
  2. Update Rule: Apply the update xk=xk−1−α⋅f′(xk−1)x_{k} = x_{k-1} - \alpha \cdot f'(x_{k-1}) iteratively.
  3. Convergence Criterion: Check the derivative magnitude, objective decrease, domain validity, and an iteration limit. Small changes in xx alone do not establish optimality.

Example​

  • For f(x)=ex−log⁡(x)f(x) = e^x - \log(x), starting from an initial guess, iterations proceed by computing the derivative at the current point and updating the point according to the learning rate and the computed gradient.
  • This process does not require solving for when the derivative equals zero but rather iteratively adjusts based on the gradient's direction and magnitude.

A domain-safe calculation​

Here log⁡\log is the natural logarithm and the domain is x>0x>0. The curvature f′′(x)=ex+1/x2>0f''(x)=e^x+1/x^2>0 makes ff strictly convex. Moreover, ff tends to infinity as x→0+x\to0^+ or x→∞x\to\infty, so it has a unique global minimizer. Solving xex=1xe^x=1 gives x∗≈0.567143x_*\approx0.567143 (also denoted W(1)W(1)); the warning about competing local minima does not apply to this example.

From x0=1x_0=1 with α=0.1\alpha=0.1, g0=e−1g_0=e-1 and x1≈0.828172x_1\approx0.828172, lowering ff from 2.7182822.718282 to 2.4776652.477665. But from x0=2x_0=2 with α=1\alpha=1, the next point is negative, so evaluating its logarithm is invalid. Reject infeasible steps before evaluating the objective. The following backtracking search halves the step until it satisfies the Armijo decrease condition; the iteration and trial caps report failure rather than claiming convergence.

from math import exp, log, isfinite

def f(x):
return exp(x) - log(x)

x = 1.0
for iteration in range(10000):
g = exp(x) - 1 / x
if abs(g) <= 1e-7:
break
alpha = 1.0
for trial in range(60):
candidate = x - alpha * g
if candidate > 0 and isfinite(candidate):
try:
value = f(candidate)
except OverflowError:
value = float("inf")
if isfinite(value) and value <= f(x) - 1e-4 * alpha * g * g:
break
alpha *= 0.5
else:
raise RuntimeError("line search failed")
x = candidate
else:
raise RuntimeError("iteration limit reached")
print(round(x, 6), round(f(x), 6))

The output is 0.567143 2.330366. The derivative tolerance certifies approximate stationarity here; strict convexity identifies the only stationary point as the global minimum. For a nonconvex objective, a small derivative could instead signal a saddle or maximum.

For a step-size benchmark, f(x)=x2f(x)=x^2 gives xk+1=(1−2α)xkx_{k+1}=(1-2\alpha)x_k. For nonzero initial xx, convergence requires 0<α<10<\alpha<1; at α=1\alpha=1 it oscillates, and above 11 it diverges. Conversely, an extremely small α\alpha can make the iterate change tiny while ∣f′(x)∣|f'(x)| remains large. If ∣f′(u)−f′(v)∣≤L∣u−v∣|f'(u)-f'(v)|\le L|u-v| on an interval containing the step (an LL-Lipschitz gradient), the descent bound is f(x−αg)≤f(x)−α(1−Lα/2)g2f(x-\alpha g)\le f(x)-\alpha(1-L\alpha/2)g^2, so 0<α<2/L0<\alpha<2/L ensures descent when g≠0g\ne0. For the logarithmic example there is no single finite global LL on (0,∞)(0,\infty); backtracking avoids assuming one.

Explore connectionsOpen network