class

PairwiseDistance

extendsModule
PairwiseDistance(p: float = 2.0, eps: float = 1e-06, keepdim: bool = False)
source

Pairwise p\ell_p distance between corresponding rows of two tensors.

Computes the p\ell_p norm of the difference vector for each pair of rows:

dp(x1,x2)=x1x2+εp=(ix1,ix2,i+εp)1/pd_p(x_1, x_2) = \left\| x_1 - x_2 + \varepsilon \right\|_p = \left( \sum_i \left| x_{1,i} - x_{2,i} + \varepsilon \right|^p \right)^{1/p}

A small ε\varepsilon is added before the norm computation for numerical stability. With p=2 this yields the standard Euclidean distance; with p=1 the Manhattan distance.

Parameters

pfloat= 2.0
The exponent pp of the p\ell_p norm. Default: 2.0.
epsfloat= 1e-06
Small ε\varepsilon added to the difference for numerical stability. Default: 1e-6.
keepdimbool= False
If True, the reduced dimension is retained as a size-1 dimension in the output. Default: False.

Notes

  • Input x1: (N,D)(N, D).
  • Input x2: (N,D)(N, D).
  • Output: (N,)(N,) if keepdim=False, else (N,1)(N, 1).

Commonly used in Siamese networks and metric-learning objectives such as contrastive loss and triplet loss, where the distance between paired embeddings must be differentiable.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.PairwiseDistance(p=2.0)
>>> x1 = lucid.tensor([[1.0, 0.0], [0.0, 1.0]])
>>> x2 = lucid.tensor([[0.0, 0.0], [0.0, 0.0]])
>>> m(x1, x2)
tensor([1.0000, 1.0000])
>>> # Euclidean distance for embedding comparison in a Siamese network
>>> emb1 = lucid.randn(64, 128)
>>> emb2 = lucid.randn(64, 128)
>>> dist = nn.PairwiseDistance(p=2.0)(emb1, emb2)
>>> dist.shape
(64,)

Methods (3)

dunder

__init__

None
__init__(p: float = 2.0, eps: float = 1e-06, keepdim: bool = False)
source

Initialise the PairwiseDistance module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x1: Tensor, x2: Tensor)
source

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.