Topics / 🧠 Deep Learning II / 🧗 Level 101: Deep Learning II: Optimisation

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.

Flow: v = beta x v + grad (remember past gradients) → w = w - lr x v (smoother, faster) → zig-zags cancel (steady directions build speed)v = beta x v + gradremember past gradientsw = w - lr x vsmoother, fasterzig-zags cancelsteady directions build speed
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.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 101: Deep Learning II: Optimisation

Share on LinkedIn