335 Star 1.5K Fork 862

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cdist.md 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
luojianing 提交于 2023-07-21 15:16 . replace target=blank

Function Differences with torch.cdist

View Source On Gitee

torch.cdist

torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')

For more information, see torch.cdist.

mindspore.ops.cdist

mindspore.ops.cdist(x1, x2, p=2.0)

For more information, see mindspore.ops.cdist.

Differences

PyTorch: Compute the p-norm distance between each pair of column vectors of the two Tensors.

MindSpore: MindSpore API basically implements the same functionality as PyTorch, with a slight difference in accuracy.

Categories Subcategories PyTorch MindSpore Differences
Parameters Parameter 1 x1 x1 -
Parameter 2 x2 x2 -
Parameter 3 p p -
Parameter 4 compute_mode - torch specifies whether to calculate the Euclidean distance by using matrix multiplication, which is not available in MindSpore

Code Example 1

# PyTorch
import torch
import numpy as np

x =  torch.tensor(np.array([[1.0, 1.0], [2.0, 2.0]]).astype(np.float32))
y =  torch.tensor(np.array([[3.0, 3.0], [3.0, 3.0]]).astype(np.float32))
output = torch.cdist(x, y, 2.0)
print(output)
# tensor([[2.8284, 2.8284],
#         [1.4142, 1.4142]])

# MindSpore
import mindspore.numpy as np
from mindspore import Tensor
from mindspore import ops

x = Tensor(np.array([[1.0, 1.0], [2.0, 2.0]]).astype(np.float32))
y = Tensor(np.array([[3.0, 3.0], [3.0, 3.0]]).astype(np.float32))
output = ops.cdist(x, y, 2.0)
print(output)
# [[2.828427  2.828427 ]
#  [1.4142135 1.4142135]]
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891