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 · ∇LStep 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
paramsandgradsare lists or 1-D arrays of the same length, andlris a float of 0 or more.- Return a new 1-D NumPy array of floats with the updated weights.
- Leave
paramsunchanged.
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.
Worked solution
Give it a real try first. Then read the solution and why each line is there.