快捷方式

PairwiseDistance

class torch.nn.PairwiseDistance(p=2.0, eps=1e-06, keepdim=False)[原始碼][原始碼]

計算輸入向量之間,或輸入矩陣的各列之間的成對距離。

距離使用 p-範數計算,如果 p 為負數,則加上常數 eps 以避免除以零,即:

dist(x,y)=xy+ϵep,\mathrm{dist}\left(x, y\right) = \left\Vert x-y + \epsilon e \right\Vert_p,

其中 ee 是由 1 組成的向量,p-範數由下式給出。

xp=(i=1nxip)1/p.\Vert x \Vert _p = \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}.
參數
  • p (實數, 可選) – 範數的階數。可以是負數。預設值:2

  • eps (float, 可選) – 避免除以零的小值。預設值:1e-6

  • keepdim (bool, 可選) – 確定是否保留向量維度。預設值:False

形狀
  • Input1: (N,D)(N, D)(D)(D),其中 N = 批次維度D = 向量維度

  • 輸入2:(N,D)(N, D)(D)(D),與輸入1形狀相同

  • 輸出:(N)(N)()(),取決於輸入維度。 如果 keepdimTrue,則為 (N,1)(N, 1)(1)(1),取決於輸入維度。

範例:
>>> pdist = nn.PairwiseDistance(p=2)
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> output = pdist(input1, input2)

文件

取得 PyTorch 的完整開發人員文件

檢視文件

教學

取得初學者和進階開發人員的深入教學

檢視教學

資源

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

檢視資源