All problems
Hinge Loss Subgradient
Problem
Compute the mean subgradient of the hinge loss with respect to w and b for labels in {-1, +1}.
For each sample with margin y_i * (b + x_i . w) < 1, subtract y_i / n from grad_b and y_i * x_ij / n from grad_w[j]. Return [grad_b, grad_w]; empty X returns [0.0, [0.0] * len(w)].
Hint
Samples with margin at least 1 contribute nothing to the subgradient.
Test cases
case 1
in: [[[1],[2]],[1,-1],[0],0]
expected: [0,[0.5]]
case 2
in: [[[1]],[1],[2],0]
expected: [0,[0]]
case 3
in: [[[1,1],[2,-1]],[1,1],[0,0],0]
expected: [-1,[-1.5,0]]
case 4
in: [[],[],[1,2],0]
expected: [0,[0,0]]
Starter codePython
def hinge_subgradient(X, y, w, b):
# Returns [grad_b, grad_w]
# Your code here
pass4 test cases · run your code against them in the editor