快捷方式

CosineEmbeddingLoss

class torch.nn.CosineEmbeddingLoss(margin=0.0, size_average=None, reduce=None, reduction='mean')[原始碼][原始碼]

建立一個衡量損失的準則(criterion),輸入為張量 x1x_1x2x_2 和標籤 Tensor yy,標籤值為 1 或 -1。使用 (y=1y=1) 最大化兩個輸入的餘弦相似度,否則使用 (y=1y=-1)。這通常用於學習非線性嵌入或半監督學習。

每個樣本的損失函式為

loss(x,y)={1cos(x1,x2),if y=1max(0,cos(x1,x2)margin),if y=1\text{loss}(x, y) = \begin{cases} 1 - \cos(x_1, x_2), & \text{if } y = 1 \\ \max(0, \cos(x_1, x_2) - \text{margin}), & \text{if } y = -1 \end{cases}
引數
  • margin (float, 可選) – 應為 1-111 之間的數字,建議取值 000.50.5。如果省略 margin,則預設值為 00

  • size_average (bool, 可選) – 已棄用(請參閱 reduction)。預設情況下,損失會按批次中的每個損失元素求平均值。請注意,對於某些損失,每個樣本有多個元素。如果欄位 size_average 設定為 False,則改為對每個 mini-batch 的損失求和。當 reduceFalse 時,此引數被忽略。預設值:True

  • reduce (bool, 可選) – 已棄用(請參閱 reduction)。預設情況下,損失會根據 size_average 對每個 mini-batch 的觀測值求平均或求和。當 reduceFalse 時,將返回每個批次元素的損失,並忽略 size_average。預設值:True

  • reduction (str, 可選) – 指定應用於輸出的歸約方式:'none' | 'mean' | 'sum''none':不應用任何歸約;'mean':輸出的總和將除以輸出中的元素數量;'sum':輸出將被求和。注意:size_averagereduce 正在被棄用,在此期間,指定這兩個引數中的任何一個都會覆蓋 reduction。預設值:'mean'

形狀
  • Input1: (N,D)(N, D)(D)(D),其中 N 是批次大小,D 是嵌入維度。

  • Input2: (N,D)(N, D)(D)(D),形狀與 Input1 相同。

  • Target: (N)(N)()()

  • Output: 如果 reduction'none',則形狀為 (N)(N),否則為標量。

示例

>>> loss = nn.CosineEmbeddingLoss()
>>> input1 = torch.randn(3, 5, requires_grad=True)
>>> input2 = torch.randn(3, 5, requires_grad=True)
>>> target = torch.ones(3)
>>> output = loss(input1, input2, target)
>>> output.backward()

文件

獲取 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並解答疑問

檢視資源