torch.cdist¶
- torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')[源代码]¶
计算两个行向量集合中每对向量之间的批量p范数距离。
- Parameters
x1 (张量) – 形状为 的输入张量。
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。
- Return type
如果 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]])