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]])