# cdist¶

paddle. cdist ( x, y, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary', name=None ) [source]

Compute the p-norm distance between each pair of the two collections of inputs.

This function is equivalent to scipy.spatial.distance.cdist(input,’minkowski’, p=p) if $$p \in (0, \infty)$$. When $$p = 0$$ it is equivalent to scipy.spatial.distance.cdist(input, ‘hamming’) * M. When $$p = \infty$$, the closest scipy function is scipy.spatial.distance.cdist(xn, lambda x, y: np.abs(x - y).max()).

Parameters
• x (Tensor) – A tensor with shape $$B \times P \times M$$.

• y (Tensor) – A tensor with shape $$B \times R \times M$$.

• p (float, optional) – The value for the p-norm distance to calculate between each vector pair. Default: $$2.0$$.

• compute_mode (str, optional) –

The mode for compute distance.

• use_mm_for_euclid_dist_if_necessary , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible.

• use_mm_for_euclid_dist , for p = 2.0, it will use matrix multiplication to calculate euclid distance.

• donot_use_mm_for_euclid_dist , it will not use matrix multiplication to calculate euclid distance.

Default: use_mm_for_euclid_dist_if_necessary.

• 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 x has shape $$B \times P \times M$$ and y has shape $$B \times R \times M$$ then the output will have shape $$B \times P \times R$$.

Examples

>>> import paddle
>>> x = paddle.to_tensor([[0.9041,  0.0196], [-0.3108, -2.4423], [-0.4821,  1.059]], dtype=paddle.float32)
>>> y = paddle.to_tensor([[-2.1763, -0.4713], [-0.6986,  1.3702]], dtype=paddle.float32)
>>> distance = paddle.cdist(x, y)
>>> print(distance)
Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[3.11927032, 2.09589314],
[2.71384072, 3.83217239],
[2.28300953, 0.37910119]])