class

MultiLabelSoftMarginLoss

extendsModule
MultiLabelSoftMarginLoss(weight: Tensor | None = None, reduction: str = 'mean')
source

Multi-label soft-margin loss (BCE with logits averaged over classes).

Treats each class as an independent binary classification problem and applies numerically stable binary cross-entropy with logits across all CC classes, then averages over the class dimension:

L(x,y)=1Cc=1C[yclogσ(xc)+(1yc)log(1σ(xc))]\mathcal{L}(x, y) = -\frac{1}{C} \sum_{c=1}^{C} \Bigl[ y_c \log \sigma(x_c) + (1 - y_c) \log(1 - \sigma(x_c)) \Bigr]

where σ\sigma is the sigmoid function. If weight is provided, each class term is multiplied by weight[c].

Parameters

weightTensor of shape (C,)= None
Manual rescaling weight per class.
reductionstr= 'mean'
'none' | 'mean' (default) | 'sum'.

Attributes

weightTensor or None
Per-class weight tensor.
reductionstr
The reduction mode.

Notes

  • Input x : (N,C)(N, C) — raw logits.
  • Target y : (N,C)(N, C) — binary labels in {0,1}\{0, 1\}.
  • Output : scalar for 'mean' / 'sum'; (N,)(N,) for 'none'.
  • This loss is appropriate when each sample can belong to multiple classes simultaneously (multi-label classification), as opposed to CrossEntropyLoss which assumes mutually exclusive classes.
  • The input is expected to be raw logits; do not apply sigmoid before passing to this module.

Examples

Multi-label classification with 4 classes:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MultiLabelSoftMarginLoss()
>>> logits = lucid.tensor([[1.5, -0.5, 0.3, 2.1],
...                        [0.2,  1.0, -1.5, 0.8]])
>>> targets = lucid.tensor([[1.0, 0.0, 1.0, 1.0],
...                         [0.0, 1.0,  0.0, 0.0]])
>>> loss = criterion(logits, targets)
With class-frequency re-weighting:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MultiLabelSoftMarginLoss(
...     weight=lucid.tensor([1.0, 2.0, 1.0, 0.5])
... )
>>> logits  = lucid.tensor([[0.5, 1.0, -0.3, 0.8]])
>>> targets = lucid.tensor([[1.0, 0.0,  1.0, 1.0]])
>>> loss = criterion(logits, targets)

Methods (3)

dunder

__init__

None
__init__(weight: Tensor | None = None, reduction: str = 'mean')
source

Initialise the MultiLabelSoftMarginLoss module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor, target: Tensor)
source

Compute the loss between predictions and targets.

Parameters

xTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.