pairwise_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 norm, as the
reference does. Default 1e-6.keepdimbool= FalseIf
True, retain the reduced dimension with size 1.
Default False.Returns
TensorDistance tensor.
Notes
The eps shift keeps coincident points off the norm's kink. With
eps=0 they sit on it, and their gradient is 0 — the subgradient
the reference picks — not nan. The shift used to be added to
|x1 - x2| instead, which differs by up to 2 * eps per entry
where x1 < x2.
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.])