Skip to content
DeepForgeby svx
All problems
MediumOptimizationop-318

Elastic Net Gradient Vector

Problem

The elastic net penalty combines L1 and L2: lambda1 * ||w||_1 + (lambda2 / 2) * ||w||^2, whose gradient adds lambda1 * sign(w) + lambda2 * w.

Given the data gradient, the weights, lambda1, and lambda2, return the penalized gradient.

Hint

Combine both penalty gradients term by term.

Test cases

case 1
in: [[1,2],[1,1],0.1,0.2]
expected: [1.3,2.3000000000000003]
case 2
in: [[0,0],[-1,0],0.5,0.5]
expected: [-1,0]
case 3
in: [[3],[0],1,0]
expected: [3]
Starter codePython
def elastic_net_gradient_vector(grad, weights, lambda1, lambda2):
    # Your code here
    pass

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