SGD with Momentum
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 vectorvelocity: 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
8 employers weight this skill
4 big tech firms, 2 quant funds, 1 frontier lab, 1 autonomy company. Top match scores 91.
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