Skip to content
fn

multi_margin_loss

→Tensor
multi_margin_loss(input: Tensor, target: Tensor, p: int = 1, margin: float = 1.0, weight: Tensor | None = None, size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = '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

inputTensor
Class scores of shape (N,C)(N, C), or (C,)(C,) for one unbatched sample (the "none" loss then takes the target's shape, () or (1,)). Any other rank raises ValueError.
targetTensor
Integer class indices of shape (N,)(N,); any other shape raises ValueError. An index outside [0,C)[0, C) raises IndexError for a CPU tensor and makes the loss NaN on Metal.
pint= 1
Power applied to each hinge term — 1 for the standard hinge loss, 2 for the smoother squared-hinge variant (default 1). Any other value raises ValueError.
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,), of input's dtype (else RuntimeError). Each sample contribution is scaled by the weight of its true class. Any other shape raises ValueError.
reductionstr= 'mean'
"mean" (default), "sum", or "none".
size_averagestr= 'mean'
"mean" (default), "sum", or "none".
reducestr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

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

Notes

Per-sample loss:

Li=1C∑j≠timax⁡ ⁣(0,  margin−xi,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.)

Used by 2