Skip to content
DeepForgeby svx
All problems
EasyOptimizationop-316

L2 Regularized Gradient

Problem

Adding an L2 penalty (lambda / 2) * ||w||^2 to a loss adds lambda * w to the gradient.

Given the data gradient and the weights, return the regularized gradient for the supplied lambda.

Hint

The penalty gradient is linear in the weights.

Test cases

case 1
in: [[1,2],[3,4],0.1]
expected: [1.3,2.4]
case 2
in: [[0,0],[1,-1],1]
expected: [1,-1]
case 3
in: [[2],[0],5]
expected: [2]
Starter codePython
def l2_regularized_gradient(grad, weights, lam):
    # Your code here
    pass

3 test cases · run your code against them in the editor