All problems
L1 Regularized Subgradient
Problem
Adding an L1 penalty lambda * ||w||_1 adds lambda * sign(w) to the gradient, using the subgradient 0 at w = 0.
Given the data gradient and the weights, return the regularized subgradient.
Hint
The subgradient of |w| is the sign of w, with zero chosen at the kink.
Test cases
case 1
in: [[1,2],[3,4],0.5]
expected: [1.5,2.5]
case 2
in: [[0,0,0],[0,-2,0],1]
expected: [0,-1,0]
case 3
in: [[-1],[5],0.25]
expected: [-0.75]
Starter codePython
def l1_regularized_subgradient(grad, weights, lam):
# Your code here
pass3 test cases · run your code against them in the editor