fn

multi_margin_loss

Tensor
multi_margin_loss(x: Tensor, target: Tensor, p: int = 1, margin: float = 1.0, weight: Tensor | None = None, reduction: str = 'mean')
source

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

xTensor
Class scores of shape (N,C)(N, C).
targetTensor
Integer class indices of shape (N,)(N,).
pint= 1
Power applied to each hinge term — 1 for the standard hinge loss, 2 for the smoother squared-hinge variant (default 1).
marginfloat= 1.0
Required minimum score gap between the true class and every competitor (default 1.0).
weightTensor or None= None
Per-class weight vector of shape (C,)(C,). Each sample contribution is scaled by the weight of its true class.
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or per-sample tensor of shape (N,)(N,).

Notes

Per-sample loss:

Li=1Cjtimax ⁣(0,  marginxi,ti+xi,j)pL_i = \frac{1}{C} \sum_{j \ne t_i} \max\!\big(0,\; \text{margin} - x_{i, t_i} + x_{i, j}\big)^p

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)