pairwise_distance

paddle.nn.functional. pairwise_distance ( x, y, p=2.0, epsilon=1e-06, keepdim=False, name=None ) [source]

It computes the pairwise distance between two vectors. The distance is calculated by p-oreder norm:

\[\Vert x \Vert _p = \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}.\]
Parameters
  • x (Tensor) – Tensor, shape is \([N, D]\) or \([D]\), where \(N\) is batch size, \(D\) is the dimension of vector. Available dtype is float32, float64.

  • y (Tensor) – Tensor, shape is \([N, D]\) or \([D]\), where \(N\) is batch size, \(D\) is the dimension of vector. Available dtype is float32, float64.

  • p (float, optional) – The order of norm. Default: \(2.0\).

  • epsilon (float, optional) – Add small value to avoid division by zero. Default: \(1e-6\).

  • keepdim (bool, optional) – Whether to reserve the reduced dimension in the output Tensor. The result tensor is one dimension less than the result of |x-y| unless keepdim is True. Default: False.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

Tensor, the dtype is same as input tensor. - If keepdim is True, the output shape is \([N, 1]\) or \([1]\),

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/nn/functional/distance.py:docstring of paddle.nn.functional.distance.pairwise_distance, line 31)

Unexpected indentation.

depending on whether the input has data shaped as \([N, D]\).

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/functional/distance.py:docstring of paddle.nn.functional.distance.pairwise_distance, line 32)

Block quote ends without a blank line; unexpected unindent.

  • If keepdim is False, the output shape is \([N]\) or \([]\),

    depending on whether the input has data shaped as \([N, D]\).

Examples

import paddle
x = paddle.to_tensor([[1., 3.], [3., 5.]], dtype=paddle.float64)
y = paddle.to_tensor([[5., 6.], [7., 8.]], dtype=paddle.float64)
distance = paddle.nn.functional.pairwise_distance(x, y)
print(distance.numpy()) # [5. 5.]