class
MultiMarginLoss
extends
ModuleMultiMarginLoss(p: int = 1, margin: float = 1.0, weight: Tensor | None = None, reduction: Reduction = 'mean')Multi-class hinge (SVM-style) margin loss.
For each sample with predicted scores and correct class index , the per-sample loss sums hinge contributions from all incorrect classes:
When weight is provided, each class contribution is scaled by
weight[y].
Parameters
pint= 1Exponent of the hinge term;
1 (default) gives the standard
hinge, 2 gives the squared hinge.marginfloat= 1.0Minimum required score gap between the true class and all others.
Default
1.0.Manual rescaling weight per class.
reductionstr= 'mean''none' | 'mean' (default) | 'sum'.Attributes
pintHinge exponent.
marginfloatThe score margin.
weightTensor or NonePer-class weight tensor.
reductionstrThe reduction mode.
Notes
- Input
x: — class scores. - Target
y: — integer class indices in . - Output : scalar for
'mean'/'sum'; for'none'.
- This is the multi-class generalisation of the SVM hinge loss.
- With
p=2the loss is the squared hinge, which penalises margin violations more aggressively.
Examples
Standard hinge loss over 3 classes:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MultiMarginLoss(margin=1.0)
>>> scores = lucid.tensor([[0.3, 2.1, 0.5], [1.8, 0.2, 1.0]])
>>> targets = lucid.tensor([1, 0])
>>> loss = criterion(scores, targets)
Squared hinge with class weights:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MultiMarginLoss(
... p=2,
... weight=lucid.tensor([1.0, 2.0, 1.0]),
... )
>>> scores = lucid.tensor([[0.5, 1.5, 0.1]])
>>> targets = lucid.tensor([1])
>>> loss = criterion(scores, targets)Used by 1
Constructors
1Instance methods
2Return a string representation of the layer's configuration.