fn
pairwise_distance
→Tensorpairwise_distance(x1: Tensor, x2: Tensor, p: float = 2.0, eps: float = 1e-06, keepdim: bool = False)Element-wise distance between two equally-shaped tensors.
Computes the per-row norm of the difference — the standard metric in metric-learning triplet and contrastive losses.
Parameters
x1TensorTensors of the same shape. Distance is reduced over the last
dimension.
x2TensorTensors of the same shape. Distance is reduced over the last
dimension.
pfloat= 2.0Order of the norm.
2 for Euclidean, 1 for Manhattan.
Default 2.0.epsfloat= 1e-06Small constant added to
|x1 - x2| before the power so the
derivative is finite even at coincident points. Default 1e-6.keepdimbool= FalseIf
True, retain the reduced dimension with size 1.
Default False.Returns
TensorDistance tensor.
Notes
The eps shift inside the norm matters for gradient stability — at
the bare distance is non-differentiable
when ; the offset converts the kink into a finite-slope
smooth point.
Examples
>>> import lucid
>>> from lucid.nn.functional import pairwise_distance
>>> a = lucid.tensor([[1.0, 2.0]])
>>> b = lucid.tensor([[4.0, 6.0]])
>>> pairwise_distance(a, b, p=2.0)
Tensor([5.0000])