Skip to content
fn

multilabel_margin_loss

→Tensor
multilabel_margin_loss(input: Tensor, target: Tensor, size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean')

Multi-label hinge loss for set-valued targets.

The multi-label counterpart of multi_margin_loss: each sample can belong to several classes (the "positives"), and the objective requires every positive class score to exceed every non-positive (negative) class score by at least 1. Used for set prediction tasks such as image tagging where labels are not mutually exclusive.

Targets are encoded as a fixed-width index list with -1 as a padding sentinel — entries up to the first -1 mark the positive classes for that sample; that entry and every one after it are ignored, whatever they hold.

Parameters

inputTensor
Class scores of shape (N,C)(N, C) or (C,)(C,); any other rank raises ValueError.
targetTensor
Same shape as input (any other raises ValueError), any integer dtype. The entries before the first negative one are the positive class indices. A listed class outside [0,C)[0, C) raises IndexError for a CPU tensor and makes that sample's loss NaN on Metal.
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,) (a 0-d tensor for a 1-D input) under "none".

Notes

Per-sample loss, summed over positive labels tt and non-positive labels jj:

Li=1C∑t∈Pi∑j∉Pimax⁡ ⁣(0,  1−xi,t+xi,j),L_i = \frac{1}{C} \sum_{t \in P_i} \sum_{j \notin P_i} \max\!\big(0,\; 1 - x_{i, t} + x_{i, j}\big),

where PiP_i is the list of positive labels for sample ii (a class listed twice is counted twice). Equivalently, it is the average of multi-class hinge losses obtained by treating each positive label as the correct one against the full set of non-positives.

Examples

>>> import lucid
>>> from lucid.nn.functional import multilabel_margin_loss
>>> scores = lucid.tensor([[1.0, 0.5, -0.3, 0.2]])
>>> target = lucid.tensor([[0, 1, -1, -1]])
>>> multilabel_margin_loss(scores, target)
tensor(0.275)

Used by 2