Implement the Gradient Descent Update

~10 mincode completion

Implement gradient_step that applies one gradient descent update to an array of parameters.

Watch out for:

  • The sign: we subtract the gradient (going downhill)
  • Modifying the original array vs. returning a new one

Examples

Correct update with lr=0.1

Input
gradient_step([2, -1], [4, -2], 0.1)
Output
[1.6, -0.8]

Zero gradients leave params unchanged

Input
gradient_step([5, 3], [0, 0], 0.5)
Output
[5, 3]

Gradient sign error detection

Input
gradient_step([1], [1], 0.1)
Output
[0.9]

Hints

Hint 1

Work directly with the arguments params, gradients, learning_rate and return the result rather than printing it.

Hint 2

Watch for this: gradient sign error.

Requirements

  • params: Current parameter values, shape (n,)

  • gradients: Gradient of loss w.r.t. params, shape (n,)

  • learning_rate: Step size alpha > 0

  • Return Updated parameters after one gradient step, shape (n,)

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

3 employers weight this skill

1 big tech firm, 1 frontier lab, 1 quant fund. Top match scores 80.

Python
import numpy as np

def gradient_step(
    params: np.ndarray,
    gradients: np.ndarray,
    learning_rate: float
) -> np.ndarray:
    """
    Apply one gradient descent step.

    Args:
        params:        Current parameter values, shape (n,)
        gradients:     Gradient of loss w.r.t. params, shape (n,)
        learning_rate: Step size alpha > 0

    Returns:
        Updated parameters after one gradient step, shape (n,)
    """
    # 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.