labs / lab-c-adam-w
Next task →
Practical Labs/Lab: Adam and AdamW: Adaptive Moment Estimation
Level 3 · optimization

Lab: Adam and AdamW: Adaptive Moment Estimation

Build a minimal, verifiable implementation that demonstrates Adam and AdamW: Adaptive Moment Estimation. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.

Mathematical basis

For Adam and AdamW: Adaptive Moment Estimation, implement θt+1=θt−η∇f(θt)\theta_{t+1}=\theta_t-\eta\nabla f(\theta_t) and check the descent lemma bound η<2/L\eta<2/L.

Instructions

  1. 01Read section 7 of the Adam and AdamW: Adaptive Moment Estimation learning page and write its governing equation on paper.
  2. 02Implement the core operation in PyTorch / NumPy on a 2D ill-conditioned quadratic. Do not call a library function that solves the whole task.
  3. 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
  4. 04Run the experiment with the learning rate η and record gradient norm.
  5. 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.

Starter code

import numpy as np

def gd(f, g, x0, lr=.05, n=200):
    x = np.array(x0, float).copy()
    for _ in range(n):
        x -= lr * g(x)
    return x

f = lambda w: .5*w[0]**2 + 5*w[1]**2
g = lambda w: np.array([w[0], 10*w[1]])
w = gd(f, g, [10., 10.])
assert np.linalg.norm(w) < 1e-2, w
print(w)