297. Gradient Descent

The gradient tells us the direction of steepest increase

For a function

𝑓(𝑥1,𝑥2,,𝑥𝑛)

the gradient of 𝑓, written 𝑓, is:

𝑓(𝑥)=(𝜕𝑓𝜕𝑥1,𝜕𝑓𝜕𝑥2,,𝜕𝑓𝜕𝑥𝑛)
Example

Function

𝑓(𝑥1,𝑥2)=𝑥12+𝑥22

Compute 𝜕𝑑𝜕𝑥1

  • Differentiate 𝑓 w.r.t. 𝑥1 (treat 𝑥2 as constant):
𝜕𝜕𝑥1(𝑥12+𝑥22)=𝜕𝜕𝑥1(𝑥12)+𝜕𝜕𝑥1(𝑥22)
  • Since 𝑥22 is constant w.r.t. 𝑥1, its derivative is 0:
𝜕𝜕𝑥1(𝑥22)=0
  • The derivative of 𝑥12 w.r.t. 𝑥1 is:
𝜕𝜕𝑥1(𝑥12)=2𝑥1
  • So,
𝜕𝑓𝜕𝑥1=2𝑥1

Compute 𝜕𝑓𝜕𝑥2

  • Differentiate 𝑓 w.r.t. 𝑥2 (treat 𝑥2 as constant):
𝜕𝜕𝑥2(𝑥12+𝑥22)=𝜕𝜕𝑥2(𝑥12)+𝜕𝜕𝑥2(𝑥22)
  • Since 𝑥12 is constant w.r.t. 𝑥2, its derivative is 0:
𝜕𝜕𝑥2(𝑥12)=0
  • The derivative of 𝑥22 w.r.t. 𝑥2 is:
𝜕𝜕𝑥2(𝑥22)=2𝑥2

So,

𝜕𝑓𝜕𝑥2=2𝑥2

Combine to form gradient

𝑓(𝑥1,𝑥2)=(2𝑥1,2𝑥2)

Evaluate at a point

𝑥=(1,2)𝑓(1,2)=(2×1,2×2)=(2,4)

Interpretation

  • The gradient vector (2,4) at (1,2) points in the direction where the function 𝑓 increases most rapidly

  • Its magnitude 22+42=204.47 measures the steepness of that increase

  • Moving one unit along the 𝑥1 axis alone would increase the function by the partial derivative with respect to 𝑥1, which is 2

  • Moving one unit along the 𝑥2 axis alone would increase the function by the partial derivative with respect to 𝑥2, which is 4

  • Moving from (1,2) in the direction of (2,4), the function value will rise fastest and at a rate roughly proportional to 4.47 per unit distance moved

297.0.1. Algorithm

Find the minimum of a function

In gradient descent, we move in the opposite direction of the gradient to minimize a function

Direction of maximum increase:

𝑓(𝑥)

Direction of maximum decrease:

𝑓(𝑥)
  1. Initialize: Start with an initial guess for the parameters
  2. Compute Gradient: Find the gradient of the function at the current parameters
  3. Update Parameters: Adjust the parameters by moving in the opposite direction of the gradient, scaled by the learning rate
  4. Repeat: Continue the process until the parameters converge to a minimum or the changes are minimal

Update Rule:

𝑥𝑘+1𝑥𝑘𝛼𝑘𝑓(𝑥𝑘)

Where:

gradient_descent.py
import numpy as np

def gradient_descent(
  f,              # Function to minimize, f(x)
  grad_f,         # Gradient of f, grad_f(x)
  x0,             # Initial guess for x
  alpha=0.01,     # Learning rate
  max_iter=1000,  # Maximum number of iterations
  tol=1e-6        # Tolerance for stopping
):
    x = x0.copy()
    for _ in range(max_iter):
        grad = grad_f(x)
        if np.linalg.norm(grad) < tol:
            break
        x -= alpha * grad
    return x

def f(x):
  """Example: minimize f(x) = x^2 + 2x + 1"""
  return x**2 + 2*x + 1

def grad_f(x):
    """Gradient of f(x) = x^2 + 2x + 1 is f'(x) = 2x + 2"""
    return 2*x + 2

gradient_descent(
    f,
    grad_f,
    x_initial,
    alpha=0.1,
    max_iter=100
)
Example

Function

𝑥12+𝑥22

Gradient

𝑓(𝑥)=(𝜕𝑓𝜕𝑥1,𝜕𝑓𝜕𝑥2)=(2𝑥1,2𝑥2)

Learning Rate

𝛼=0.1

Iteration 1

  • Initial Parameter Values:
𝑥1=1𝑥2=2
  • Gradient Calculation
𝑓(𝑥)=(2×1,2×2)=(2,4)
  • Parameter Update:
𝑥𝑥𝛼𝑓(𝑥)𝑥110.1×2=0.8𝑥220.1×4=1.6
  • Updated Parameter Values:
𝑥1=0.8𝑥2=1.6

Iteration 2

  • Current Parameter Values
𝑥1=0.8𝑥2=1.6
  • Gradient Calculation:
𝑓(𝑥)=(2×0.8,2×1.6)=(1.6,3.2)
  • Parameter Update:
𝑥𝑥𝛼𝑓(𝑥)𝑥10.80.1×1.6=0.64𝑥21.60.1×3.2=1.28
  • Updated Parameter Values:
𝑥1=0.64𝑥2=1.28

Iteration 3

  • Current Values
𝑥1=0.64𝑥2=1.28
  • Gradient Calculation:
𝑓(𝑥)=(2×0.64,2×1.28)=(1.28,2.56)
  • Parameter Update:
𝑥𝑥𝛼𝑓(𝑥)𝑥10.640.1×1.28=0.512𝑥21.280.1×2.56=1.024
  • Updated Parameter Values:
𝑥1=0.512𝑥2=1.024
Example

Problem Setup

Minimize the quadratic function:

min𝑥2𝑓(𝑥)=4𝑥124𝑥1𝑥2+2𝑥22where𝑥=[𝑥1𝑥2]

Gradient of 𝑓(𝑥)

The gradient is:

𝑓(𝑥)=[𝜕𝑓𝜕𝑥1𝜕𝑓𝜕𝑥2]

Step 1: Compute 𝜕𝑓𝜕𝑥1

Take the derivative of each term with respect to 𝑥1 (treat 𝑥2 as a constant):

  • 𝜕𝜕𝑥1(4𝑥12)=8𝑥1

  • 𝜕𝜕𝑥1(4𝑥1𝑥2)=4𝑥2

  • 𝜕𝜕𝑥1(2𝑥22)=0

So:

𝜕𝑓𝜕𝑥1=8𝑥14𝑥2

Step 2: Find 𝜕𝑓𝜕𝑥2

Take the derivative of each term with respect to 𝑥2 (treat 𝑥1 as a constant):

  • 𝜕𝜕𝑥2(4𝑥12)=0

  • 𝜕𝜕𝑥2(4𝑥1𝑥2)=4𝑥1

  • 𝜕𝜕𝑥2(2𝑥22)=4𝑥2

So:

𝜕𝑓𝜕𝑥2=4𝑥1+4𝑥2

So the gradient of 𝑓(𝑥) is:

𝑓(𝑥)=[8𝑥14𝑥24𝑥1+4𝑥2]

Optimal Solution

𝑥=[00]𝑓(𝑥)=0

Gradient Descent Iterations

Iteration 1

  • Initial guess:
𝑥0=[23]𝑓(𝑥0)=4(22)4(2)(3)+2(32)=10
  • Gradient at 𝑥0:
𝑓(𝑥0)=[8(2)4(3)4(2)+4(3)]=[44]
  • Line Search:
𝑥(𝛼0)=𝑥0𝛼0𝑓(𝑥0)=[23]𝛼0[44]=[24𝛼034𝛼0]𝑓(𝑥(𝛼0))=4𝑥124𝑥1𝑥2+2𝑥22=4(24𝛼0)24(24𝛼0)(34𝛼0)+2(34𝛼0)2=32𝛼0232𝛼0+10
  • Minimizing:
𝛼0=argmin𝑓(𝑥(𝛼))𝜕𝜕𝛼𝑓(𝑥(𝛼0))=𝜕𝜕𝛼(32𝛼0232𝛼0+10)=0=64𝛼032=0𝛼0=12
  • Update Step:
𝑥1=𝑥0𝑎0𝑓(𝑥0)=[23]12[44]=[01]
  • Check Progress:
  1. New point:
𝑥1=[01]
  1. Function value decreases 𝑓(𝑥𝑘+1)<𝑓(𝑥𝑘)
𝑓(𝑥1)=2<𝑓(𝑥0)=10
  1. Gradient at new point:
𝑓(𝑥1)=[8(0)4(1)4(0)+4(1)]=[44]
  1. Gradient magnitude 𝑓(𝑥𝑘+1)<𝑓(𝑥𝑘)
𝑓(𝑥1)=42=𝑓(𝑥0)

Note: Gradient magnitude stays the same in this iteration due to the specific structure of this quadratic function

Iteration 2

  • New Point:
𝑥1=[01]
  • Gradient at 𝑥1:
𝑓(𝑥1)=[8(0)4(1)4(0)+4(1)]=[44]𝑎1=argmin𝑓(𝑥(𝛼1))
  • Line search:
𝑥(𝛼1)=𝑥1𝛼1𝑓(𝑥1)=[01]𝛼1[44]=[4𝛼114𝛼1]𝑓(𝑥(𝛼1))=4𝑥124𝑥1𝑥2+2𝑥22=4(4𝛼1)24(4𝛼1)(14𝛼1)+2(14𝛼1)2=160𝛼1232𝛼1+2
  • Minimize:
𝛼1=argmin𝑓(𝑥(𝛼1))=110𝜕𝜕𝛼𝑓(𝑥(𝛼1))=𝜕𝜕𝛼(160𝛼1232𝛼1+2)=0=320𝛼132=0𝛼1=110
  • Update Step:
𝑥2=𝑥1𝛼1𝑓(𝑥1)=[01]110[44]=[0.40.6]
  • Improvement:
  1. New point:
𝑥2=[0.40.6]
  1. Function value decreases 𝑓(𝑥𝑘+1)<𝑓(𝑥𝑘)
𝑓(𝑥2)=0.4<𝑓(𝑥1)=2
  1. Gradient at new point:
𝑓(𝑥2)=[8(0.4)4(0.6)4(0.4)+4(0.6)]=[0.80.8]
  1. Gradient magnitude 𝑓(𝑥𝑘+1)<𝑓(𝑥𝑘)
𝑓(𝑥2)=(0.8,0.8)=425
Example

Problem Setup

Minimize the quadratic function:

min𝑥2𝑓(𝑥)=𝑥122𝑥1𝑥2+2𝑥22+2𝑥1where𝑥=[𝑥1𝑥2]

Gradient of 𝑓(𝑥)

𝑓(𝑥1,𝑥2)=[2𝑥12𝑥2+22𝑥1+4𝑥2]