All problems
Multiclass Hinge Gradient
Problem
For the multiclass hinge loss with scores s and correct label index y, the loss is max_j (1 + s_j - s_y) over j != y, capped at zero.
The gradient with respect to s is +1 at the best violating class, -1 at the correct class (when a violation exists), and zeros elsewhere. Given scores and the label index, return the gradient list.
Hint
Only the single highest-scoring violator receives the positive gradient.
Test cases
case 1
in: [[3,1,0],0]
expected: [0,0,0]
case 2
in: [[1,3,0],0]
expected: [-1,1,0]
case 3
in: [[0,0,0],1]
expected: [1,-1,0]
Starter codePython
def multiclass_hinge_gradient(scores, label):
# Your code here
pass3 test cases · run your code against them in the editor