hinge_embedding_loss(input: Tensor, target: Tensor, margin: float = 1.0, size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = 'mean')Hinge embedding loss.
Designed for similarity learning on pre-computed distances:
given a (typically non-negative) score input representing a
pairwise distance, push positive pairs (label )
toward small distances and negative pairs (label )
above a fixed margin. Common in Siamese network training
and energy-based dissimilarity models.
Parameters
inputTensorPer-pair score (distance) tensor, any shape.
targetTensorLabel tensor with the same shape as
input (one
that does not broadcast with it raises ValueError).marginfloat= 1.0Margin enforced for negative pairs (default
1.0).reductionstr= 'mean'"mean" (default), "sum", or "none".size_ averagestr= 'mean'"mean" (default), "sum", or "none".reducestr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or full-shape per reduction.
Notes
Per-element loss:
The positive branch simply minimises the distance; the negative branch is a one-sided hinge that pushes apart only those pairs whose distance is below the margin — pairs already far apart contribute nothing. This asymmetric structure prevents the loss from collapsing all embeddings into a single point.
Examples
>>> import lucid
>>> from lucid.nn.functional import hinge_embedding_loss
>>> dist = lucid.tensor([0.2, 0.8])
>>> y = lucid.tensor([1.0, -1.0])
>>> hinge_embedding_loss(dist, y, margin=1.0)
tensor(0.2)