fn
margin_ranking_loss
→Tensormargin_ranking_loss(x1: Tensor, x2: Tensor, y: Tensor, margin: float = 0.0, reduction: str = 'mean')Pairwise ranking hinge loss.
Trains a scoring function so that for each pair
the signed score gap exceeds the
margin. Used for learning-to-rank (search, recommendation),
Bradley-Terry style preference modelling, and reward-model
training for RLHF — wherever the supervision signal is a
pairwise preference rather than a target value.
Parameters
x1TensorScores for the first item of each pair, any shape.
x2TensorScores for the second item, same shape as
x1.yTensorPairwise preference label : if
should rank higher, otherwise.
marginfloat= 0.0Required minimum score gap (default
0.0).reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or per-pair tensor.
Notes
Per-pair loss:
Pairs already satisfying the margin () contribute zero loss and zero gradient — the hinge structure naturally focuses learning on the violating pairs, akin to a pairwise SVM.
Examples
>>> import lucid
>>> from lucid.nn.functional import margin_ranking_loss
>>> s1 = lucid.tensor([2.0, 0.5])
>>> s2 = lucid.tensor([1.0, 1.0])
>>> y = lucid.tensor([1.0, 1.0])
>>> margin_ranking_loss(s1, s2, y, margin=1.0)
Tensor(0.75)