fn

multilabel_margin_loss

Tensor
multilabel_margin_loss(x: Tensor, target: Tensor, reduction: str = 'mean')
source

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.

Parameters

xTensor
Class scores of shape (N,C)(N, C) or (C,)(C,).
targetTensor
Same shape as x. Non-negative entries are positive class indices; -1 entries are ignored.
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

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

Notes

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

Li=1CtPijPimax ⁣(0,  1xi,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 set of positive labels for sample ii. 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]], dtype=lucid.int32)
>>> multilabel_margin_loss(scores, target)
Tensor(0.85)