Skip to content
DeepForgeby svx
All problems
MediumOptimizationop-025

L1 Proximal Soft-Threshold

Problem

Apply the L1 proximal operator (soft thresholding) after a gradient step.

First compute z = w - lr * grad, then shrink it by the threshold t = lr * lam: return z - t if z > t, z + t if z < -t, and 0.0 otherwise. This is the proximal step for L1-regularized objectives.

Hint

Values inside the threshold window collapse to exactly zero.

Test cases

case 1
in: [1,0.5,0.1,0.1]
expected: 0.94
case 2
in: [0.05,0,0.1,1]
expected: 0
case 3
in: [-2,-0.5,0.1,0.2]
expected: -1.93
case 4
in: [0,0,0.1,0.5]
expected: 0
Starter codePython
def l1_soft_threshold(w, grad, lr, lam):
    # Your code here
    pass

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