Nadam: One Adam Step That Looks Ahead

OptimizationMedium+20 XP+15

Nadam: One Adam Step That Looks Ahead

Nadam is Adam with Nesterov's look-ahead folded into the first moment — instead of stepping along the momentum you already have, you step along where it's heading.

1m = beta1 * m + (1 - beta1) * grad
2v = beta2 * v + (1 - beta2) * grad²
3
4m_hat = m / (1 - beta1^t)
5v_hat = v / (1 - beta2^t)
6
7lookahead = beta1 * m_hat + (1 - beta1) * grad / (1 - beta1^t)
8param = param - learning_rate * lookahead / (sqrt(v_hat) + epsilon)

Task: write nadam_step(params, grads, m, v, learning_rate, beta1, beta2, epsilon, timestep) returning [new_params, new_m, new_v], rounded to 4 decimal places.

  • timestep starts at 1.
  • Return the moments before bias correction.

Compare the first test case against the AdaGrad and AdamW problems on identical inputs. The moments come out the same as AdamW's, but the parameter moves further — the look-ahead term is weighting the current gradient more heavily than the accumulated average does.

Examples

params = [1.0]
grads = [2.0]
m = [0.0]
v = [0.0]
learning_rate = 0.01
beta1 = 0.9
beta2 = 0.999
epsilon = 1e-08
timestep = 1
returns [[0.981], [0.2], [0.004]]
params = [0.5, -0.5]
grads = [1.0, -1.0]
m = [0.0, 0.0]
v = [0.0, 0.0]
learning_rate = 0.1
beta1 = 0.9
beta2 = 0.999
epsilon = 1e-08
timestep = 1
returns [[0.31, -0.31], [0.1, -0.1], [0.001, 0.001]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run