Skip to content
DeepForgeby svx
All problems
EasyCalculusca-218

Subgradient of Hinge Loss

Problem

The hinge loss is max(0, 1 - y * w dot x) with y in {+1, -1}.

A subgradient with respect to w is -y*x when the margin y*w dot x is below 1, and 0 otherwise (choose 0 at the kink). Given w, x, and y, return the subgradient vector.

Hint

Only points inside or on the margin produce a nonzero subgradient.

Test cases

case 1
in: [[1,0],[1,0],1]
expected: [0,0]
case 2
in: [[0,0],[1,2],1]
expected: [-1,-2]
case 3
in: [[0,0],[1,2],-1]
expected: [1,2]
case 4
in: [[1,1],[1,1],1]
expected: [0,0]
Starter codePython
def subgradient_hinge(w, x, y):
    # Your code here
    pass

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