評價此頁

CosineEmbeddingLoss#

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

建立一個損失函式,用於計算輸入張量 x1x_1, x2x_2 and a 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, optional) – 應該是一個介於 1-111 之間的數字,建議使用 000.50.5。如果 margin 未提供,則預設值為 00

  • size_average (bool, optional) – 已棄用 (參見 reduction)。預設情況下,損失值在批次中的每個損失元素上取平均值。請注意,對於某些損失,每個樣本有多個元素。如果欄位 size_average 設定為 False,則損失值在每個小批次中而是求和。當 reduceFalse 時忽略。預設值:True

  • reduce (bool, optional) – 已棄用 (參見 reduction)。預設情況下,損失值在每個小批次中根據 size_average 對觀測值進行平均或求和。當 reduceFalse 時,返回每個批次元素的損失值,並忽略 size_average。預設值:True

  • reduction (str, optional) – 指定要應用於輸出的約簡方式:'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()
forward(input1, input2, target)[原始碼]#

執行前向傳播。

返回型別

張量