快捷方式

CosineSimilarity

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

返回 x1x_1x2x_2 之間的餘弦相似度,沿 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, 可選) – 計算餘弦相似度的維度。預設值:1

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

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

  • 輸入2: (1,D,2)(\ast_1, D, \ast_2),與 x1 具有相同維度數,在 dim 維度上與 x1 大小匹配,

    並且在其他維度上可以與 x1 廣播。

  • 輸出: (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 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得解答

檢視資源