class
MarginRankingLoss
extends
ModuleMarginRankingLoss(margin: float = 0.0, reduction: str = 'mean')Margin ranking loss for pairwise ranking problems.
Given two inputs , and a binary label
, the loss enforces that the correctly ranked
input exceeds the other by at least margin:
When , should be larger than (e.g. a more relevant document score). When , should be larger.
Parameters
marginfloat= 0.0Minimum required score gap between the two inputs.
Default
0.0.reductionstr= 'mean''none' | 'mean' (default) | 'sum'.Attributes
marginfloatThe margin threshold.
reductionstrThe reduction mode.
Notes
- x1 : or .
- x2 : or .
- y : with values in .
- Output : scalar for
'mean'/'sum'; same shape as input for'none'.
- Commonly used in learning-to-rank tasks such as information retrieval and recommendation systems.
- A positive margin requires a minimum gap between
scores; setting it to
0only penalises inversions.
Examples
Ranking pairs with a margin of 0.3:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MarginRankingLoss(margin=0.3)
>>> x1 = lucid.tensor([1.0, 0.5, 2.0])
>>> x2 = lucid.tensor([0.5, 1.0, 1.5])
>>> y = lucid.tensor([1.0, -1.0, 1.0])
>>> loss = criterion(x1, x2, y)
Strict ranking (no margin):
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MarginRankingLoss()
>>> x1 = lucid.tensor([3.0, 1.0])
>>> x2 = lucid.tensor([2.0, 2.0])
>>> y = lucid.tensor([1.0, 1.0])
>>> loss = criterion(x1, x2, y)Methods (3)
dunder
__init__
→None__init__(margin: float = 0.0, reduction: str = 'mean')Initialise the MarginRankingLoss module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x1: Tensor, x2: Tensor, y: Tensor)Compute the loss between predictions and targets.
Parameters
x1TensorInput tensor.
x2TensorInput tensor.
yTensorInput tensor.
Returns
TensorScalar loss (or unreduced tensor depending on reduction).
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.