評價此頁

HingeEmbeddingLoss#

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

測量給定輸入張量 xx 和標籤張量 yy (包含 1 或 -1) 的損失。這通常用於衡量兩個輸入是否相似或不相似,例如使用 L1 成對距離作為 xx,並且通常用於學習非線性嵌入或半監督學習。

對於小批次中第 nn 個樣本的損失函式為

ln={xn,if  yn=1,max{0,marginxn},if  yn=1,l_n = \begin{cases} x_n, & \text{if}\; y_n = 1,\\ \max \{0, margin - x_n\}, & \text{if}\; y_n = -1, \end{cases}

總損失函式為

(x,y)={mean(L),if reduction=‘mean’;sum(L),if reduction=‘sum’.\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}

其中 L={l1,,lN}L = \{l_1,\dots,l_N\}^\top.

引數
  • margin (float, optional) – 預設為 1

  • 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'

形狀
  • 輸入:()(*),其中 * 表示任意數量的維度。求和操作作用於所有元素。

  • 目標:()(*),與輸入形狀相同

  • 輸出:標量。如果 reduction'none',則與輸入形狀相同

forward(input, target)[原始碼]#

執行前向傳播。

返回型別

張量