fn
multi_margin_loss
→Tensormulti_margin_loss(x: Tensor, target: Tensor, p: int = 1, margin: float = 1.0, weight: Tensor | None = None, reduction: str = 'mean')Multi-class hinge (margin) loss — Crammer-Singer SVM objective.
A non-probabilistic alternative to cross_entropy for
multi-class classification: instead of fitting a softmax
distribution, it requires the true-class score to exceed every
other class score by at least margin. Frequently used in
structured prediction and as a drop-in for hinge-style losses
in metric learning.
Parameters
xTensorClass scores of shape .
targetTensorInteger class indices of shape .
pint= 1Power applied to each hinge term —
1 for the standard
hinge loss, 2 for the smoother squared-hinge variant
(default 1).marginfloat= 1.0Required minimum score gap between the true class and
every competitor (default
1.0).weightTensor or None= NonePer-class weight vector of shape . Each sample
contribution is scaled by the weight of its true class.
reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or per-sample tensor of shape .
Notes
Per-sample loss:
Samples whose true-class score already dominates all
competitors by margin produce zero loss and zero gradient
— like the binary SVM hinge, only the support vectors
contribute to the update.
Examples
>>> import lucid
>>> from lucid.nn.functional import multi_margin_loss
>>> scores = lucid.tensor([[2.0, 0.5, 0.1]])
>>> target = lucid.tensor([0], dtype=lucid.int32)
>>> multi_margin_loss(scores, target)
Tensor(0.0)