class paddle.nn. PairwiseDistance ( 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}.\]
  • 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.

  • x: \([N, D]\) or \([D]\), where \(N\) is batch size, \(D\) is the dimension of the data. Available data type is float16, float32, float64.

  • y: \([N, D]\) or \([D]\), y have the same dtype as x.

  • output: The same dtype as input tensor.
    • If keepdim is True, the output shape is \([N, 1]\) or \([1]\), depending on whether the input has data shaped as \([N, D]\).

    • If keepdim is False, the output shape is \([N]\) or \([]\), depending on whether the input has data shaped as \([N, D]\).


>>> import paddle
>>> x = paddle.to_tensor([[1., 3.], [3., 5.]], dtype=paddle.float64)
>>> y = paddle.to_tensor([[5., 6.], [7., 8.]], dtype=paddle.float64)
>>> dist = paddle.nn.PairwiseDistance()
>>> distance = dist(x, y)
>>> print(distance)
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
[4.99999860, 4.99999860])
forward ( x, y )


Defines the computation performed at every call. Should be overridden by all subclasses.

  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments

extra_repr ( )


Extra representation of this layer, you can have custom implementation of your own layer.