torch.cdist#
- torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary')[原始碼]#
計算兩個行向量集合之間每對向量的 p-範數距離(批處理)。
- 引數
x1 (Tensor) – 輸入張量,最後兩個維度分別代表點和特徵維度。形狀可以是 , 其中 是點的數量, 是特徵維度。
x2 (Tensor) – 輸入張量,最後兩個維度也分別代表點和特徵維度。形狀可以是 , 其中 是點的數量, 是特徵維度,該維度應與 x1 的特徵維度匹配。
p (float) – 用於計算每個向量對之間 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]])