One gradient descent step

Easy8 min10 hidden tests

A churn model keeps one weight per feature. After each batch, the training code hands you the gradient of the loss for every weight. Write gradient_step(params, grads, lr) that returns the weights after one gradient descent update: each weight moves against its gradient by lr times that gradient.

Pick the learning rate

L(w) = ½(1.5 w₁² + 4 w₂²) · w ← w − lr · ∇L

Step 10 · loss 0.031

Examples

Both weights move against their gradients

gradient_step([2.0, -1.0], [4.0, -2.0], 0.1)

returns [1.6, -0.8]

A zero gradient leaves a weight where it is

gradient_step([0.5, 0.5, 0.5], [0.0, 0.0, 0.0], 0.3)

returns [0.5, 0.5, 0.5]

Requirements

  • params and grads are lists or 1-D arrays of the same length, and lr is a float of 0 or more.
  • Return a new 1-D NumPy array of floats with the updated weights.
  • Leave params unchanged.
The idea behind it

Training a model means lowering a loss: a number that says how wrong the model is on a batch. The gradient of the loss is a vector with one entry per weight. Each entry says how fast the loss grows when that weight grows a little. Gradient descent takes a small step the other way: w_new = w - lr * grad. The learning rate lr sets the step size. Too small and training crawls. Too large and the loss jumps around or blows up. Frameworks such as PyTorch compute the gradients for you with backpropagation, and an optimizer applies this update to millions of weights at once, thousands of times per training run.

pythonCtrl+Enter runs
import numpy as np

def gradient_step(params, grads, lr):
"""One gradient descent update.

params: list or 1-D array of n weights
grads: list or 1-D array of n gradients (how fast the loss grows per weight)
lr: learning rate, a float of 0 or more
Returns a new 1-D float array with the n updated weights.
"""
raise NotImplementedError

Stuck? Reveal one rung at a time

Worked solution

Give it a real try first. Then read the solution and why each line is there.