SGD with Momentum

~20 mincode completion

Implement sgd_momentum_update(weights, velocity, gradient, learning_rate, momentum). Return (weights_new, velocity_new) as a tuple.

Examples

Updated weights (accessor [0]): cold start

Input
sgd_momentum_update([1, 2], [0, 0], [1, 1], 0.1, 0.9)
[0] of result
[0.9, 1.9]

Updated velocity (accessor [1]): cold start

Input
sgd_momentum_update([1, 2], [0, 0], [1, 1], 0.1, 0.9)
[1] of result
[0.1, 0.1]

Warm start: prior velocity carries forward

Input
sgd_momentum_update([0, 0], [0.5, 0.5], [1, 0], 0.1, 0.9)
[0] of result
[-0.55, -0.45]

Hints

Hint 1

Work directly with the arguments weights, velocity, , learning_rate, and return the result rather than printing it.

Hint 2

A common slip here: added velocity instead of subtracting.

Requirements

  • weights: Current weight vector

  • velocity: Current velocity vector (same shape as weights)

  • : Current gradient

  • learning_rate: Step size (alpha)

  • : Momentum coefficient (beta), typically 0.9

  • Return Tuple (weights_new, velocity_new).

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~20 min

8 employers weight this skill

4 big tech firms, 2 quant funds, 1 frontier lab, 1 autonomy company. Top match scores 91.

Python
import numpy as np

def sgd_momentum_update(weights: np.ndarray, velocity: np.ndarray, gradient: np.ndarray,
                        learning_rate: float, momentum: float):
    """
    Perform one SGD + momentum update.

    Args:
        weights:       Current weight vector
        velocity:      Current velocity vector (same shape as weights)
        gradient:      Current gradient
        learning_rate: Step size (alpha)
        momentum:      Momentum coefficient (beta), typically 0.9

    Returns:
        Tuple (weights_new, velocity_new).
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.