oneflow.nn.PairwiseDistance

class oneflow.nn.PairwiseDistance(p: Optional[float] = 2.0, eps: Optional[float] = 1e-06, keepdim: Optional[bool] = False)

Computes the pairwise distance between vectors \(v_1\), \(v_2\) using the p-norm:

\[\left \| x \right \| _p = (\sum_{i=1}^n \left | x_i \right |^p )^{\frac{1}{p}}\]

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.PairwiseDistance.html.

Parameters
  • p (real) – the norm degree. Default: 2

  • eps (float, optional) – Small value to avoid division by zero. Default: 1e-6

  • keepdim (bool, optional) – Determines whether or not to keep the vector dimension. Default: False

Shape:
  • Input1: \((N, D)\) or \((D)\), where N = batch dimension and D = vector dimension

  • Input2: \((N, D)\) or \((D)\), same shape as the input1

  • Output: \((N)\) or \(()\) based on input dimension. If keepdim is True, then \((N, 1)\) or \((1)\) based on input dimension.

For example:

>>> import oneflow as flow
>>> pdist = flow.nn.PairwiseDistance(p=2)
>>> x1 = flow.arange(12).reshape(3, 4)
>>> x2 = flow.arange(12).reshape(3, 4)
>>> pdist(x1, x2)
tensor([2.0000e-06, 2.0000e-06, 2.0000e-06], dtype=oneflow.float32)
>>> pdist(x1, x2).shape
oneflow.Size([3])