快捷方式

torch.cdist

torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')[源代码][源代码]

计算两组行向量中每对向量之间的批量 p-范数距离。

参数
  • x1 (Tensor) – 输入张量,形状为 B×P×MB \times P \times M

  • x2 (Tensor) – 输入张量,形状为 B×R×MB \times R \times M

  • p (float) – 用于计算每对向量之间的 p-范数距离的 p 值 [0,]\in [0, \infty]

  • compute_mode (str) – 'use_mm_for_euclid_dist_if_necessary' - 如果 P > 25 或 R > 25,将使用矩阵乘法方法计算欧氏距离 (p = 2)。 'use_mm_for_euclid_dist' - 总是使用矩阵乘法方法计算欧氏距离 (p = 2)。 'donot_use_mm_for_euclid_dist' - 绝不使用矩阵乘法方法计算欧氏距离 (p = 2)。 默认值: use_mm_for_euclid_dist_if_necessary。

返回类型

Tensor

如果 x1 的形状为 B×P×MB \times P \times M 且 x2 的形状为 B×R×MB \times R \times M,则输出的形状为 B×P×RB \times P \times R

此函数等价于 scipy.spatial.distance.cdist(input,’minkowski’, p=p)(当 p(0,)p \in (0, \infty) 时)。当 p=0p = 0 时,等价于 scipy.spatial.distance.cdist(input, ‘hamming’) * M。当 p=p = \infty 时,最接近的 scipy 函数是 scipy.spatial.distance.cdist(xn, lambda x, y: np.abs(x - y).max())

示例

>>> a = torch.tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]])
>>> a
tensor([[ 0.9041,  0.0196],
        [-0.3108, -2.4423],
        [-0.4821,  1.0590]])
>>> b = torch.tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]])
>>> b
tensor([[-2.1763, -0.4713],
        [-0.6986,  1.3702]])
>>> torch.cdist(a, b, p=2)
tensor([[3.1193, 2.0959],
        [2.7138, 3.8322],
        [2.2830, 0.3791]])

文档

访问 PyTorch 的完整开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源