torch.cdist¶
- torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')[源代码][源代码]¶
计算两组行向量中每对向量之间的批量 p-范数距离。
- 参数
x1 (Tensor) – 输入张量,形状为 。
x2 (Tensor) – 输入张量,形状为 。
p (float) – 用于计算每对向量之间的 p-范数距离的 p 值 。
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。
- 返回类型
如果 x1 的形状为 且 x2 的形状为 ,则输出的形状为 。
此函数等价于 scipy.spatial.distance.cdist(input,’minkowski’, p=p)(当 时)。当 时,等价于 scipy.spatial.distance.cdist(input, ‘hamming’) * M。当 时,最接近的 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]])