Python exercise · about 5 minutes · free
SGD With Momentum: Keep Rolling
Plain gradient descent zig-zags across a narrow valley. A velocity term smooths the zig-zag and speeds up the long direction.
Momentum keeps a running velocity of past gradients: v = beta x v + grad, then w = w - lr x v. Directions where the gradient keeps the same sign build up speed; directions that flip sign cancel out. Typical beta is 0.9. Nesterov momentum looks ahead before computing the gradient.
Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.
Your quest: Write
momentum_step(w, v, grad, lr, beta) returning (new_w, new_v), and descend(grad_fn, w0, lr, beta, steps) that starts with velocity 0, applies the step steps times (the gradient is grad_fn(w) at the current weight) and returns the final weight.Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
<code>v_new = beta * v + grad; w_new = w - lr * v_new</code>. In <code>descend</code> keep both in a loop.Output appears here.