快速鍵

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 是批次大小 (batch size); dd 是一個非負實值函數,用於量化兩個張量 (tensor) 之間的接近程度,稱為 distance_function (距離函數);而 marginmargin 是一個非負邊界 (margin),代表正距離和負距離之間所需的最小差異,才能使損失 (loss) 為 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 距離作為距離函數,計算輸入張量的 triplet loss。

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

  • margin (float, optional) – 一個非負的邊界值 (margin),表示 loss 為 0 時,正距離和負距離之間所需的最小差值。較大的邊界值會懲罰負樣本相對於正樣本而言,與 anchor 的距離不夠遠的情況。預設值: 11

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

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

形狀
  • 輸入: (N,)(N, *) 其中 * 表示距離函數支援的任何其他維度數量。

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

範例

>>> # 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 等人: Learning shallow convolutional feature descriptors with triplet losses: https://bmva-archive.org.uk/bmvc/2016/papers/paper119/index.html

文件

存取 PyTorch 的完整開發者文件

檢視文件

教學

取得針對初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並獲得您問題的解答

檢視資源