Multi-label & Margin Losses
- Distinguish the {0, 1} and {-1, +1} label conventions and select the correct margin or logistic loss for each
- Apply SoftMarginLoss and MultiLabelSoftMarginLoss for single-label and multi-label binary classification respectively
- Use MultiMarginLoss as a multi-class hinge loss and explain the margin constraint it enforces between the correct and incorrect class scores
- Select among SoftMarginLoss, MultiLabelSoftMarginLoss, MultiMarginLoss, and MultiLabelMarginLoss using the decision guide for label type and output cardinality
Label Conventions: {0,1} vs {−1,+1}
Classification losses use two incompatible label conventions:
- {0, 1} — used by BCE-family losses. means "positive", means "negative".
- {−1, +1} — used by margin-based losses. means "positive", means "negative".
Mixing these up is a silent bug: the wrong convention produces valid-looking numbers but trains in the wrong direction.
nn.SoftMarginLoss — Binary Logistic Loss with {−1,+1} Labels
SoftMarginLoss implements the logistic loss for labels :
This is equivalent to BCEWithLogitsLoss under the label remapping . When and is large, and (correct and confident). When and is very negative, is huge and the loss is large.
loss = nn.SoftMarginLoss()
x = torch.tensor([2.0, -1.0, 0.5])
y = torch.tensor([1.0, -1.0, 1.0]) # labels in {-1, +1}
output = loss(x, y)
When to use: Binary classification where labels are naturally {−1, +1} (e.g., SVM-style data); direct replacement for BCEWithLogitsLoss when switching from {0,1} to {−1,+1} labeling.
nn.MultiLabelSoftMarginLoss — Independent Binary Classifiers
For multi-label problems where each sample can belong to any subset of classes, this loss applies SoftMarginLoss independently across all classes:
This is equivalent to averaging independent BCEWithLogitsLoss values. Labels (not {−1, +1} despite the "Soft Margin" name).
loss = nn.MultiLabelSoftMarginLoss()
x = torch.randn(3, 5) # 3 samples, 5 classes
y = torch.zeros(3, 5).random_(2) # binary multi-label targets
output = loss(x, y)
When to use: Multi-label image classification (e.g., an image can be both "dog" and "outdoor"); tagging tasks; any setting where classes are not mutually exclusive.
nn.MultiMarginLoss — Multi-class Hinge Loss
Hinge loss for single-label -class classification. Encourages the correct class score to exceed all incorrect class scores by a margin :
where is the correct class, is a power exponent, and by default. This is the multi-class SVM loss (also called "Crammer & Singer loss").
The loss is zero when for all wrong classes — i.e., when the correct class has a sufficient margin over all others. Otherwise it penalises proportionally.
loss = nn.MultiMarginLoss(p=1, margin=1.0)
x = torch.randn(4, 10) # logits
y = torch.tensor([0, 3, 7, 2]) # correct class indices
output = loss(x, y)
When to use: When you want an SVM-style margin objective instead of softmax cross-entropy; structured prediction where inter-class margins matter.
nn.MultiLabelMarginLoss — Multi-label Pairwise Ranking
For multi-label problems where you have a set of correct class indices. It enforces that each positive class scores higher than each negative class by a margin of 1:
where is the set of positive class indices and is the set of negative class indices.
Target format: a 1-D LongTensor of positive class indices, padded with to a fixed length.
loss = nn.MultiLabelMarginLoss()
x = torch.FloatTensor([[0.1, 0.2, 0.4, 0.8]]) # 1 sample, 4 classes
y = torch.LongTensor([[3, 0, -1, -1]]) # classes 3 and 0 are positive
output = loss(x, y)
When to use: Document retrieval or recommendation where you have a small set of relevant items and many irrelevant ones; ranking-oriented multi-label tasks.
Decision Guide
| Setting | Labels | Recommended Loss |
|---|---|---|
| Binary, one label per sample | BCEWithLogitsLoss | |
| Binary, labels are ±1 | SoftMarginLoss | |
| Multi-label, independent per class | MultiLabelSoftMarginLoss | |
| Multi-class, one label, SVM-style | Integer | MultiMarginLoss |
| Multi-label, ranking objective | Positive index list | MultiLabelMarginLoss |