MultiMarginLoss¶
MultiMarginLoss computes the multi-class hinge loss for classification
tasks. It encourages the score of the correct class to be greater than the
scores of all other classes by at least a specified margin.
It is commonly used in multi-class classification and margin-based learning.
In Aakaar, this loss expects one-hot encoded targets rather than integer class indices.
Signature¶
Parameters¶
| Parameter | Type | Description |
|---|---|---|
p |
int |
Power used in the loss. Supported values are 1 and 2. Default is 1. |
margin |
float |
Desired minimum margin between the correct class score and the remaining class scores. Default is 1.0. |
reduction |
str |
Specifies the reduction to apply: "mean", "sum", or "none". Default is "mean". |
Inputs¶
| Input | Description |
|---|---|
logits |
Predicted class scores. Shape is typically (batch_size, num_classes). |
target_onehot |
One-hot encoded target tensor with the same shape as logits. |
Returns¶
A scalar loss when using "mean" or "sum", or a tensor containing the
per-sample losses when using "none".
Formula¶
For each sample:
where:
score(correct)is the score of the correct class.score(other)represents every incorrect class.- The correct class itself is excluded from the computation.
The parameter p determines whether the margin penalty is linear (p = 1)
or quadratic (p = 2).
Example¶
import aakaar
from aakaar.losses import MultiMarginLoss
logits = aakaar.tensor([
[3.2, 0.5, 1.1],
[0.3, 2.8, 1.0]
])
targets = aakaar.tensor([
[1, 0, 0],
[0, 1, 0]
])
criterion = MultiMarginLoss()
loss = criterion(
logits,
targets
)
Using Quadratic Margins¶
Quadratic margins penalize violations more heavily than linear margins.
Using a Larger Margin¶
Increasing the margin requires greater separation between the correct class and the incorrect classes.
Using Different Reductions¶
Mean (default)¶
Sum¶
None¶
Returns the loss for each sample individually.
Typical Training Loop¶
Typical Applications¶
- Multi-class classification
- Margin-based classifiers
- Image classification
- Pattern recognition
- Learning-to-rank variants
- Representation learning
Notes¶
- Expects one-hot encoded targets in the current version of Aakaar.
- Supports only
p=1andp=2. - The correct class is excluded from the margin computation.
- Supports
"mean","sum", and"none"reductions. - Encourages the correct class score to exceed all incorrect class scores by at least the specified margin.