multilabel_soft_margin_loss
→Tensormultilabel_soft_margin_loss(input: Tensor, target: Tensor, weight: Tensor | None = None, reduction: str = 'mean')Per-class logistic loss averaged over labels (multi-label BCE).
The standard objective for multi-label classification with
independent per-class probabilities: each class gets its own
binary logistic regression head, and the total loss is the
mean of the per-class binary cross-entropies. Mathematically
equivalent to applying
binary_cross_entropy_with_logits per class and
averaging across the class axis.
Computed via the numerically stable identity
, which avoids
overflow / underflow for large |x|.
Parameters
inputTensortargetTensorweightTensor or None= Nonereductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or per-sample tensor of shape .
Notes
Per-sample loss, averaged across the classes:
Because the per-class predictions are independent (no softmax coupling), the gradient through each class is exactly that of a single binary logistic regression — convenient for highly multi-label problems where the active label set is sparse.
Examples
>>> import lucid
>>> from lucid.nn.functional import multilabel_soft_margin_loss
>>> logits = lucid.tensor([[2.0, -1.0, 0.5]])
>>> target = lucid.tensor([[1.0, 0.0, 1.0]])
>>> multilabel_soft_margin_loss(logits, target)
Tensor(0.3567...)