带距离的 triplet margin 损失
- classtorch.nn.TripletMarginWithDistanceLoss(*, distance_function=None, margin=1.0, swap=False, reduction='mean')[源代码]
-
创建一个准则来度量三元组损失,给定输入张量$a$、$p$和$n$(分别代表锚点、正例和反例),并使用一个非负实值函数(“距离函数”)来计算锚点与正例之间的距离(“正向距离”)以及锚点与反例之间的距离(“反向距离”)。
未减少的损失(即将
reduction
设置为'none'
的情况)可以描述为:$\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\}$其中$N$表示批量大小;$d$是非负实值函数,用于量化两个张量之间的接近程度,并被称为
distance_function
; 而$margin$是非负边界,表示正距离和负距离之间所需的最小差异,以使损失为0。输入张量各有$N$个元素,并且可以是任何形状,只要距离函数能够处理即可。如果
reduction
不是'none'
(默认值为'mean'
),则:$\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}$参见
TripletMarginLoss
,它使用$l_p$距离作为度量函数来计算输入张量的三元组损失。- 参数
-
-
distance_function (Callable, optional) – 一个用于量化两个张量之间接近程度的非负实值函数。如果没有指定,默认使用nn.PairwiseDistance。默认值:
None
-
margin (float, 可选) – 一个非负的边距,表示正样本和负样本之间的最小距离差,当这个差距满足条件时损失函数值为0。较大的边距会惩罚那些相对于正样本而言,负样本与锚点不够远的情况。默认值:$1$。
-
swap (bool, 可选) – 是否使用论文《Learning shallow convolutional feature descriptors with triplet losses》中提到的距离交换方法。如果为 True,且正样本距离负样本更近,则在损失计算时将正样本与锚点互换。默认值:
False
。 -
reduction (str, 可选) – 指定(可选)应用于输出的缩减方式:
'none'
|'mean'
|'sum'
。'none'
: 不进行任何缩减,'mean'
: 输出总和除以元素数量,'sum'
: 对输出求和。默认值:'mean'
-
- 形状:
-
-
输入: $(N, *)$,其中$*$ 表示任意数量的额外维度,这些维度由距离函数支持。
-
输出:如果
reduction
为'none'
,则输出形状为 $(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, et al.: 使用三元组损失学习浅层卷积特征描述符:http://www.bmva.org/bmvc/2016/papers/paper119/index.html