mindspore.mint.cdist

mindspore.mint.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')[source]

Compute p-norm distance between each pair of row vectors of two input tensors.

Warning

This is an experimental API that is subject to change. On Ascend, CANN 8.5.0_0120 or a later version is required.

Note

On Ascend, the supported dtypes are mindspore.float16 and mindspore.float32.

Parameters:
  • x1 (Tensor) – The input tensor with shape \((B, P, M)\). Letter \(B\) represents 0 or positive int number. When \(B\) is equal to 0, it means this dimension can be ignored, i.e. shape of the tensor is \((P, M)\).

  • x2 (Tensor) – The input tensor with shape \((B, R, M)\), has the same dtype as x1.

  • p (float, optional) – P value for the p-norm distance to calculate between each vector pair, P >= 0. Default 2.0 .

  • compute_mode (str, optional) – Specify the compute mode. Setting this parameter currently has no effect. Default 'use_mm_for_euclid_dist_if_necessary' .

Returns:

Tensor, p-norm distance, with the same dtype as x1, its shape is \((B, P, R)\).

Raises:
  • ValueError – If p is negative.

  • ValueError – If dimension of x1 is not the same as x2.

  • ValueError – If dimension of x1 or x2 is neither 2 nor 3.

  • ValueError – If the batch dim of x1 and x2 can not broadcast.

  • ValueError – If the number of columns of x1 is not the same as that of x2.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> x = mindspore.tensor([[[1.0, 1.0], [2.0, 2.0]]], dtype=mindspore.float32)
>>> y = mindspore.tensor([[[3.0, 3.0], [3.0, 3.0]]], dtype=mindspore.float32)
>>> mindspore.mint.cdist(x, y, 2.0)
Tensor(shape=[1, 2, 2], dtype=Float32, value=
[[[ 2.82842708e+00,  2.82842708e+00],
  [ 1.41421354e+00,  1.41421354e+00]]])