Plain momentum can overshoot in narrow valleys because its velocity keeps pushing the parameters forward even after the gradient has turned around.
This is the problem addressed by SGD with momentum. Nesterov momentum lets the gradient look one step ahead before committing to the parameter update.
In this exercise, use this reparameterized form of Nesterov's method:
vnew=momentum⋅v+grad paramnew=param−lr(grad+momentum⋅vnew)The gradient is evaluated at the current parameter. Initial velocity is zeros.
This is a reparameterized form of Nesterov's method. It matches the textbook update up to a change of variables, which is how PyTorch's SGD(nesterov=True) is written.
Implement:
nesterov_step(param, grad, velocity, lr, momentum)
The function returns:
(new_param, new_velocity)
The signature is provided in the editor.
param, grad, and velocity must have the same shape. A mismatch raises ValueError.momentum must be in [0, 1). Otherwise, raise ValueError.momentum = 0, the result must equal plain SGD.Compute the new velocity first.
The parameter update uses the new velocity, not the old velocity.
Write both update lines and compare them with the momentum version.
No results yet
Run your code to check it against the examples, or submit to run every test.