L2 Regularization Gradient
Easy
~10 min
code completion
Gradient of L2 Regularization
When using gradient descent, we need the gradient of the regularization term with respect to the weights:
In practice, this gradient is added to the task loss gradient before the parameter update:
This is why L2 regularization is equivalent to weight decay: the update subtracts a small fraction of the current weights at every step.
Your task:
Implement l2_gradient(weights, lambda_reg) that returns .
Example Tests
Standard gradient
Input: {"weights":[1,2,3],"lambda_reg":0.1}
Expected: [0.2,0.4,0.6]
Zero weights: zero gradient
Input: {"weights":[0,0],"lambda_reg":5}
Expected: [0,0]
lambda=0.5: 2*0.5=1 so gradient equals weights
Input: {"weights":[-1,2,-3],"lambda_reg":0.5}
Expected: [-1,2,-3]