multilabel_margin_loss(x: Tensor, target: Tensor, 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
ValueError.targetTensorx (any other raises ValueError), any
integer dtype. The entries before the first negative one are
the positive class indices. A listed class outside
raises IndexError for a CPU tensor and makes
that sample's loss NaN on Metal.reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar, or per-sample tensor of shape (a 0-d tensor
for a 1-D x) under "none".
Notes
Per-sample loss, summed over positive labels and non-positive labels :
where is the list of positive labels for sample (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)