Numerical Optimization III: Gradient Descent

Eugeniy E. Mikhailov and Greg Bentsen

Logistics and Agenda

Homework 5 due Monday Oct 26 at 11:59pm


Midterm 2


Agenda

Today

Optimization in multiple dimensions


Goal: Efficiently navigate the global energy landscape.

Hunt for global minimum (but often get stuck in local minima instead).

Where we left off

Given adjustable parameters θ→=(θ1,…,θd)\vec{\theta}=(\theta_1,\ldots,\theta_d), minimize the objective function E(θ→)E(\vec{\theta})

Two examples from last time:

  • Linear regression:
    • parameters (m,b)(m,b)
    • objective χ2(m,b)\chi^2(m,b)
  • Four-bead protein:
    • parameters (θ1,θ2)(\theta_1,\theta_2)
    • objective E(θ1,θ2)E(\theta_1,\theta_2)

Question: How do we find the bottom of a landscape without scanning every point?

Gradient-Based Optimization

The gradient: which way is uphill?

For E(θ1,θ2)E(\theta_1,\theta_2), define the gradient:

∇→E=(∂E/∂θ1∂E/∂θ2) \boxed{\vec\nabla E=\begin{pmatrix} \partial E/\partial\theta_1\\[3pt] \partial E/\partial\theta_2 \end{pmatrix}}

Source: generated with ChatGPT
Source: generated with ChatGPT

Gradient descent is an iterated map

Start with θ→0\vec{\theta}_0, then repeatedly step downhill:

θ→n+1=θ→n−η∇→E(θ→n) \boxed{\vec\theta_{n+1}=\vec\theta_n-\eta \ \vec\nabla E(\vec\theta_n)}

This is an iterated map, just like our earlier dynamical-systems examples.

Gradient descent in Python

Start with θ→0\vec{\theta}_0, then repeatedly step downhill:

θ→n+1=θ→n−η∇→E(θ→n) \boxed{\vec\theta_{n+1}=\vec\theta_n-\eta \ \vec\nabla E(\vec\theta_n)}

import numpy as np

# grad_energy(theta) takes a length-2 array and returns the gradient (also length-2)

theta = np.array([1.0, -2.0])   # initial guess (radians)
eta = 0.05                      # learning rate
tol = 1e-6                      # convergence tolerance
max_steps = 1e4                 # maximum number of steps

grad = grad_energy(theta)
step = 0

while (np.linalg.norm(grad) > tol) & (step < max_steps):
    theta -= eta * grad
    grad = grad_energy(theta)
    step = step + 1

In practice: ∇→E\vec\nabla E may be derived analytically or approximated using finite differences.

Demo: gradient descent for linear regression

First: linear-regression loss

χ2(m,b)=∑i[yi−(mxi+b)]2 \chi^2(m,b)=\sum_i[y_i-(mx_i+b)]^2

Demo: linear_fit_gradient_descent.py

Observe: Which minimum do we reach? How do the lines / protein change during descent?

Adjusting the learning rate

Takeaway: a larger step is not always faster!

Too slow, too fast, or just right?

Return to 1D and use a simple objective f(x)=x2f(x)=x^2

Newton-Raphson:

dfdx=2x⇒xn+1=(1−2η)xn \frac{df}{dx}=2x \qquad\Rightarrow\qquad \boxed{x_{n+1}=(1-2\eta)x_n}

Fixed point analysis: |F′|=|1−2η||F'| = |1-2\eta|

At η=1\eta=1, the iterates alternate with constant magnitude.

Demo: gradient descent for protein model

What changes in a more complicated landscape?

Demo: folding_gradient_descent.py

Different initial guesses: may converge to different minima

Question: How can we be confident we found the global minimum?

Other Ways to Navigate the Landscape

What if the gradient ∇→E\vec \nabla E isn’t available?

Nelder–Mead: gradient-free optimization [optional]

Some objective functions come from expensive simulations, making gradients inconvenient.

Idea: move a triangle (a simplex in two dimensions) across the landscape.

  • Evaluate ff at its three vertices
  • Reflect / expand / contract to seek lower values
  • Repeat, shrinking around a candidate minimum
Source: https://www.slideserve.com/nay/nelder-mead
Source: https://www.slideserve.com/nay/nelder-mead

Like gradient descent, Nelder–Mead is generally a local optimizer.

Pitfalls and Takeaways

Common optimization traps

Practical strategy: run the optimizer from many starting configurations.

Avoiding Local Minima

A downhill algorithm can get trapped

Source: https://commons.wikimedia.org/wiki/File%3AGradient_Descent_in_2D.webm
Source: https://commons.wikimedia.org/wiki/File%3AGradient_Descent_in_2D.webm

Movie:  Wikimedia Commons: Gradient Descent in 2D

Question: How could we make the search more reliable?

Multistart optimization: try many initial guesses

  1. Draw many starting configurations 𝛉0(k)\boldsymbol\theta_0^{(k)}
  2. Run gradient descent from each
  3. Compare final energies and keep the lowest

k*=argminkE(𝛉final(k)) \boxed{k_* = \mathop{\mathrm{argmin}}_k E\!\left(\boldsymbol\theta_{\mathrm{final}}^{(k)}\right)}

Still no mathematical guarantee of a global minimum.

AI-coding demo: automate multistart searches

Prompt a local AI model:

Starting from our four-bead protein energy(theta1, theta2, ...) function located in folding_model.py, write Python code that samples 100 random initial angle configurations theta1,theta2∈[−π,π)2theta1, theta2 \in [-\pi,\pi)^2, runs gradient descent from each, and plots the trajectories on an energy contour map. Highlight the lowest final energy. Do not change any existing code, and write this Python code in a new file named multistart_protein.py. Do not examine any files other than folding_model.py and do not leave the directory Lecture 14.

Verify its output:

Simulated annealing: sometimes go uphill!

Problem: strictly downhill steps cannot cross an energy barrier.

New strategy: propose a random change and sometimes accept an uphill move:

P(accept)=min(1,e−ΔE/T) \boxed{P(\mathrm{accept})=\min\left(1,e^{-\Delta E/T}\right)}

with

ΔE=Enew−Eold \Delta E=E_{\mathrm{new}}-E_{\mathrm{old}}


Uphill moves are especially probable at high temperature TT, but occur less frequently at low temperature.


Gradually lower TT: explore first, settle later.

Demo: hot and cold searches

Same energy landscape, different temperatures:

Connection to physics: thermal fluctuations and Boltzmann weights.

Which method should we use?

Method Needs gradients? Main strength Main limitation
Gradient descent Yes (or finite differences) Simple, efficient local descent Initial-guess and step-size sensitivity
Multistart descent Yes (repeatedly) Explores multiple basins More computational work
Nelder–Mead No Uses function values only Can stagnate; local search
Simulated annealing No Can cross energy barriers Requires many evaluations and temperature tuning

Takeaways

Next: combinatorial optimization — when our choices are discrete rather than continuous.