335 Star 1.5K Fork 861

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cdist.md 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
TingWang 提交于 2023-09-18 10:08 . update link logo

比较与torch.cdist的差异

查看源文件

torch.cdist

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

更多内容详见torch.cdist

mindspore.ops.cdist

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

更多内容详见mindspore.ops.cdist

差异对比

MindSpore此API功能与PyTorch基本一致,MindSpore无法指定是否使用矩阵乘的方式计算向量对之间的欧几里得距离。

PyTorch: 当参数 compute_modeuse_mm_for_euclid_dist_if_necessary ,且当 x1x2 的一个batch中的行向量的个数超过25时,使用矩阵乘的方式计算向量对之间的欧几里得距离。当参数 compute_modeuse_mm_for_euclid_dist 时,使用矩阵乘的方式计算向量对之间的欧几里得距离。当参数 compute_modedonot_use_mm_for_euclid_dist 时,不会使用矩阵乘的方式计算向量对之间的欧几里得距离。

MindSpore:无参数 compute_mode 以指定是否使用矩阵乘的方式计算向量对之间的欧几里得距离。在 GPUCPU 上不会使用矩阵乘的方式计算向量对之间的欧几里得距离,在 Ascend 上,会使用矩阵乘的方式计算向量对之间的欧几里得距离。

分类 子类 PyTorch MindSpore 差异
参数 参数1 x1 x1 -
参数2 x2 x2 -
参数3 p p -
参数4 compute_mode - PyTorch中指定是否用矩阵乘的方式计算欧几里得距离的参数,MindSpore中没有该参数

代码示例

# PyTorch
import torch
import numpy as np

torch.set_printoptions(precision=7)
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.8284271, 2.8284271],
#         [1.4142135, 1.4142135]])

# 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
master

搜索帮助