All problems
Subgradient Step For Absolute Value
Problem
Minimize f(x) = |x| with the subgradient method.
For n_iters iterations use g = 1 if x > 0, g = -1 if x < 0, and g = 0 when x is exactly 0, then update x = x - lr * g. Return the final value of x.
Hint
Once x reaches 0 the chosen subgradient is 0, so it stays there.
Test cases
case 1
in: [3,0.5,10]
expected: 0
case 2
in: [-2.5,0.5,4]
expected: -0.5
case 3
in: [0.2,0.1,3]
expected: 0
case 4
in: [0,0.1,5]
expected: 0
Starter codePython
def subgradient_step_abs(x, lr, n_iters):
# Minimize f(x) = |x|
# Your code here
pass4 test cases · run your code against them in the editor