multilabel_margin_loss
→Tensormultilabel_margin_loss(x: Tensor, target: Tensor, reduction: str = '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.
Parameters
xTensortargetTensorx. Non-negative entries are positive
class indices; -1 entries are ignored.reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or per-sample tensor of shape .
Notes
Per-sample loss, summed over positive labels and non-positive labels :
where is the set of positive labels for sample . 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)