Nesterov Momentum: Look Ahead Before Stepping

OptimizationEasy+10 XP+10

Nesterov Momentum: Look Ahead Before Stepping

Plain momentum builds up velocity in a consistent direction, which helps roll through noise — but it also overshoots, because it commits to a step before checking what's ahead. Nesterov momentum peeks at where the velocity is about to take it and corrects for that.

Use this formulation exactly:

1velocity = momentum * velocity + grad
2param = param - learning_rate * (grad + momentum * velocity)

Task: write nesterov_step(params, grads, velocity, learning_rate, momentum) returning [new_params, new_velocity], rounded to 4 decimal places.

  • Update the velocity first, then use the new velocity in the parameter step.
  • momentum = 0 reduces this to ordinary gradient descent.

The look-ahead term is the whole difference. Standard momentum steps along the accumulated velocity and then measures the gradient; Nesterov effectively measures it after the momentum step, so it starts braking before it overshoots rather than after.

Examples

params = [1.0]
grads = [0.5]
velocity = [0.0]
learning_rate = 0.1
momentum = 0.9
returns [[0.905], [0.5]]
params = [2.0, -1.0]
grads = [1.0, 1.0]
velocity = [0.5, 0.5]
learning_rate = 0.1
momentum = 0.9
returns [[1.7695, -1.2305], [1.45, 1.45]]

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