Multiclass Hinge Loss and Gradient

medium
Unit 14 · Losses & CNNs0/3 to milestone

The multiclass SVM (Weston–Watkins) loss for scores ss and true class yy is

Li=∑j≠yimax⁡(0, sj−syi+Δ)L_i = \sum_{j \ne y_i} \max(0,\ s_j - s_{y_i} + \Delta)

Implement hinge_loss(scores, y, delta=1.0) returning the mean over the batch, and hinge_grad(scores, y, delta=1.0) returning ∂L/∂s\partial L / \partial s (same shape as scores, already divided by the batch size). Use subgradient 0 where a margin is exactly 0.

#losses#svm#backprop2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Multiclass Hinge Loss and Gradient · TechGradify