快速鍵

CosineSimilarity

class torch.nn.CosineSimilarity(dim=1, eps=1e-08)[source][source]

傳回 x1x_1x2x_2 之間的餘弦相似度 (cosine similarity),沿著 dim 計算。

similarity=x1x2max(x12x22,ϵ).\text{similarity} = \dfrac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)}.
參數
  • dim (int, optional) – 計算餘弦相似度的維度。預設值:1

  • eps (float, optional) – 避免除以零的小數值。預設值:1e-8

形狀
  • 輸入1: (1,D,2)(\ast_1, D, \ast_2) 其中 D 位於 dim 的位置

  • Input2: (1,D,2)(\ast_1, D, \ast_2), 與 x1 具有相同的維度數量,且在維度 dim 上與 x1 的大小相符,

    並可在其他維度上與 x1 進行廣播。

  • Output: (1,2)(\ast_1, \ast_2)

範例:
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> cos = nn.CosineSimilarity(dim=1, eps=1e-6)
>>> output = cos(input1, input2)

文件

取得 PyTorch 的完整開發者文件

檢視文件

教學

取得適合初學者和進階開發者的深入教學

檢視教學

資源

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

檢視資源