fn
hinge_embedding_loss
→Tensorhinge_embedding_loss(x: Tensor, y: Tensor, margin: float = 1.0, reduction: str = 'mean')Hinge embedding loss.
Designed for similarity learning on pre-computed distances:
given a (typically non-negative) score x 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
xTensorPer-pair score (distance) tensor, any shape.
yTensorLabel tensor with the same shape as
x.marginfloat= 1.0Margin enforced for negative pairs (default
1.0).reductionstr= '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)