v9.1.1
Menu

Momentum Update Step

mediumFREEOptimization & TrainingDeep LearningDeep Learning 1

Problem Statement

Update velocity and parameters using Momentum optimization.

Background

Momentum helps accelerate gradients in the right direction and dampens oscillations. The update rules are:

v=β⋅v+(1−β)⋅dWv = \beta \cdot v + (1 - \beta) \cdot dW W=W−α⋅vW = W - \alpha \cdot v

Where:

  • vv is the velocity (exponentially weighted average of gradients)
  • β\beta is the momentum coefficient (typically 0.9)
  • α\alpha is the learning rate
  • dWdW is the gradient

Your Task

Write a function momentum_step(weight, grad, velocity, beta, learning_rate) that returns a tuple of (updated_weight, updated_velocity).

Output Format

Return a tuple (weight, velocity) with values rounded to 4 decimal places.

Example

Input
weight=0.0, grad=1.0, velocity=0.0, beta=0.9, learning_rate=0.1
Output
(-0.01, 0.1)
Reasoning
  • Inputs: a scalar parameter weight W=0W = 0, its current gradient grad dW=1dW = 1, the running velocity velocity v=0v = 0 (a first step), momentum coefficient beta β=0.9\beta = 0.9 and learning rate learning_rate α=0.1\alpha = 0.1.
  • Velocity first: the new velocity is a weighted blend of the old velocity and the fresh gradient: v′=0.9⋅0+(1−0.9)⋅1=0.1v' = 0.9 \cdot 0 + (1 - 0.9) \cdot 1 = 0.1. With v=0v = 0, only the (1−β)(1-\beta) share of the gradient survives.
  • Then the weight: the step uses the new velocity, not the old one: W′=0−0.1⋅0.1=−0.01W' = 0 - 0.1 \cdot 0.1 = -0.01.
  • Output: the pair (weight, velocity) = (-0.01, 0.1), each rounded to 4 decimal places. Note the order: weight comes first even though velocity is computed first. Rounding also hides float noise (1−0.91 - 0.9 is not exactly 0.10.1).
  • Notice: a plain gradient step would move WW by α⋅dW=0.1\alpha \cdot dW = 0.1; momentum moves it only 0.010.01 here. If the same gradient keeps arriving, vv grows toward dWdW and the steps speed up.

Constraints

  • -1000 <= weight, grad, velocity <= 1000
  • 0 <= beta < 1
  • 0 < learning_rate <= 1

Learn this in

More from Deep Learning 1

solution.py

Test results

Sign in to run your code against the test cases.