Velocity accumulation from scratch
Easy TrainingImplement SGD with momentum from scratch. Momentum accumulates a velocity — an exponentially-weighted average of past gradients — so consistent directions build up speed while oscillations across a ravine cancel out.
Implement the class below. Use only basic PyTorch operations.
Use this code to debug before submitting.
Try solving it yourself first! Click below to reveal the solution.
For interactive practice with auto-grading, run TorchCode locally:pip install torch-judge then use check("sgd_momentum")
Velocity buffer (EWA of gradients), damps oscillations in steep ravines. With a constant gradient g the velocity settles at g/(1−momentum) — a speed-up along steady directions. Covered in DL Module 7 §4.