快捷方式

torch.cdist

torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')[原始碼][原始碼]

計算兩個 row vector 集合中每對 row vector 之間的批次 p-norm 距離。

參數
  • x1 (Tensor) – 形狀為 B×P×MB \times P \times M 的輸入張量。

  • x2 (Tensor) – 形狀為 B×R×MB \times R \times M 的輸入張量。

  • p (float) – 用於計算每個向量對之間 p-norm 距離的 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

如果 p(0,)p \in (0, \infty),則此函數等同於 scipy.spatial.distance.cdist(input,’minkowski’, p=p)。當 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 的完整開發者文件

檢視文件

教學

取得針對初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並獲得您問題的解答

檢視資源