快捷方式

TripletMarginWithDistanceLoss

class torch.nn.TripletMarginWithDistanceLoss(*, distance_function=None, margin=1.0, swap=False, reduction='mean')[source][source]

建立一個準則,該準則根據輸入張量 aappnn(分別代表錨點、正樣本和負樣本),以及一個非負實值函式(“距離函式”)來衡量三元組損失。該距離函式用於計算錨點與正樣本之間的關係(“正距離”)以及錨點與負樣本之間的關係(“負距離”)。

未經約簡的損失(即 reduction 設定為 'none' 時)可表示為

(a,p,n)=L={l1,,lN},li=max{d(ai,pi)d(ai,ni)+margin,0}\ell(a, p, n) = L = \{l_1,\dots,l_N\}^\top, \quad l_i = \max \{d(a_i, p_i) - d(a_i, n_i) + {\rm margin}, 0\}

其中 NN 是批處理大小;dd 是一個非負實值函式,用於量化兩個張量之間的接近程度,被稱為 distance_functionmarginmargin 是一個非負的邊界值(margin),表示正距離與負距離之間的最小差異,當差異達到或超過此值時損失為 0。輸入張量每個有 NN 個元素,可以是距離函式可以處理的任何形狀。

如果 reduction 不是 'none'(預設為 'mean'),則

(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}

另請參閱 TripletMarginLoss,它使用 lpl_p 距離作為距離函式來計算輸入張量的三元組損失。

引數
  • distance_function (Callable, optional) – 一個非負實值函式,用於量化兩個張量之間的接近程度。如果未指定,將使用 nn.PairwiseDistance。預設值: None

  • margin (float, optional) – 一個非負邊界值(margin),表示正距離與負距離之間的最小差異,當差異達到或超過此值時損失為 0。較大的邊界值會懲罰負樣本相對於正樣本與錨點不夠遠的那些情況。預設值: 11

  • swap (bool, optional) – 是否使用 V. Balntas, E. Riba 等人的論文 Learning shallow convolutional feature descriptors with triplet losses 中描述的距離交換。如果為 True,並且正樣本比錨點更接近負樣本,則在損失計算中交換正樣本和錨點。預設值: False

  • reduction (str, optional) – 指定要應用於輸出的(可選)約簡方式: 'none' | 'mean' | 'sum''none':不應用約簡, 'mean':輸出的總和將被輸出中的元素數量除, 'sum':輸出將被求和。預設值: 'mean'

形狀
  • 輸入: (N,)(N, *) 其中 * 代表距離函式支援的任意數量的額外維度。

  • 輸出: 如果 reduction'none',則形狀為 (N)(N) 的張量,否則為標量。

示例

>>> # Initialize embeddings
>>> embedding = nn.Embedding(1000, 128)
>>> anchor_ids = torch.randint(0, 1000, (1,))
>>> positive_ids = torch.randint(0, 1000, (1,))
>>> negative_ids = torch.randint(0, 1000, (1,))
>>> anchor = embedding(anchor_ids)
>>> positive = embedding(positive_ids)
>>> negative = embedding(negative_ids)
>>>
>>> # Built-in Distance Function
>>> triplet_loss = \
>>>     nn.TripletMarginWithDistanceLoss(distance_function=nn.PairwiseDistance())
>>> output = triplet_loss(anchor, positive, negative)
>>> output.backward()
>>>
>>> # Custom Distance Function
>>> def l_infinity(x1, x2):
>>>     return torch.max(torch.abs(x1 - x2), dim=1).values
>>>
>>> triplet_loss = (
>>>     nn.TripletMarginWithDistanceLoss(distance_function=l_infinity, margin=1.5))
>>> output = triplet_loss(anchor, positive, negative)
>>> output.backward()
>>>
>>> # Custom Distance Function (Lambda)
>>> triplet_loss = (
>>>     nn.TripletMarginWithDistanceLoss(
>>>         distance_function=lambda x, y: 1.0 - F.cosine_similarity(x, y)))
>>> output = triplet_loss(anchor, positive, negative)
>>> output.backward()
參考文獻

V. Balntas 等人:學習具有三元組損失的淺層卷積特徵描述符: https://bmva-archive.org.uk/bmvc/2016/papers/paper119/index.html

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

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

檢視教程

資源

查詢開發資源並獲得問題的解答

檢視資源