损失函数总结(十):TripletMarginLoss、TripletMarginWithDistanceLoss

news/2025/2/13 17:34:32/

损失函数总结(十):TripletMarginLoss、TripletMarginWithDistanceLoss

  • 1 引言
  • 2 损失函数
    • 2.1 TripletMarginLoss
    • 2.2 TripletMarginWithDistanceLoss
  • 3 总结

1 引言

在前面的文章中已经介绍了介绍了一系列损失函数 (L1LossMSELossBCELossCrossEntropyLossNLLLossCTCLossPoissonNLLLossGaussianNLLLossKLDivLossBCEWithLogitsLossMarginRankingLossHingeEmbeddingLossMultiMarginLossMultiLabelMarginLossSoftMarginLossMultiLabelSoftMarginLoss)。在这篇文章中,会接着上文提到的众多损失函数继续进行介绍,给大家带来更多不常见的损失函数的介绍。这里放一张损失函数的机理图:
在这里插入图片描述

2 损失函数

2.1 TripletMarginLoss

论文链接:Learning local feature descriptors with triplets and shallow convolutional neural networks

TripletMarginLoss 是一种损失函数,不同于交叉熵损失仅仅考虑样本与类别标签之间误差,TripletMarginLoss 关注样本与其他样本之间距离。在输入张量 x1、x2、x3 和边际值大于 0 的情况下,创建一个用于测量三元组损失的标准。一个三元组由 a、p 和 n(即分别为正样本负样本)组成。所有输入张量的形状应为 (N,D)小批量每个样品的损失函数为::
L ( a , p , n ) = m a x { d ( a i , p i ) − d ( a i , n i ) + m a r g i n , 0 } L(a,p,n)=max\{d(a_i,p_i)−d(a_i,n_i)+margin,0\} L(a,p,n)=max{d(ai,pi)d(ai,ni)+margin,0}

其中:
d ( x i , y i ) = ∥ x i − y i ∥ p d(x_i,y_i)=∥x_i−y_i∥_p d(xi,yi)=xiyip

  • 损失函数目标:最小化损失函数,使得锚点与正例的距离越小,与负例的距离越大。
  • m a r g i n margin margin: 人为设置的常数。

代码实现(Pytorch):

triplet_loss = nn.TripletMarginLoss(margin=1.0, p=2, eps=1e-7)
anchor = torch.randn(100, 128, requires_grad=True)
positive = torch.randn(100, 128, requires_grad=True)
negative = torch.randn(100, 128, requires_grad=True)
output = triplet_loss(anchor, positive, negative)
output.backward()

在siamese net或者Triplet net任务中被广泛使用。。。。

2.2 TripletMarginWithDistanceLoss

TripletMarginWithDistanceLoss 函数与 TripletMarginLoss功能基本一致,只不过可以定制化的传入不同的距离函数。当传入的距离函数是torch.nn.PairwiseDistance时,两者完全一致。

使用自定义损失函数,代码实现(Pytorch):

import torch
import torch.nn as nn
import torch.nn.functional as Ftorch.manual_seed(20)
triplet_loss = nn.TripletMarginLoss(margin=1.0, p=2)
anchor = torch.randn(100, 128, requires_grad=True)
positive = torch.randn(100, 128, requires_grad=True)
negative = torch.randn(100, 128, requires_grad=True)# 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)
print(output.item())# 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)
print(output.item())

输出结果:

1.529929518699646
1.0007251501083374

在siamese net或者Triplet net任务中被广泛使用。。。。

3 总结

到此,使用 损失函数总结(十) 已经介绍完毕了!!! 如果有什么疑问欢迎在评论区提出,对于共性问题可能会后续添加到文章介绍中。如果存在没有提及的损失函数也可以在评论区提出,后续会对其进行添加!!!!

如果觉得这篇文章对你有用,记得点赞、收藏并分享给你的小伙伴们哦😄。


http://www.ppmy.cn/news/1186900.html

相关文章

fl studio哪个版本好?fl studio21汉化破解版2024免费下载安装图文激活教程

fl studio哪个版本好?fl studio21汉化破解版2024免费下载安装图文激活教程 fl studio21破解版下载专区由兔八哥爱分享小编精心为大家整理了fl studio软件所有版本合集。其中包含了fl studio21修改版、fl studio 20汉化修改版、fl studio21中文修改版、水果fl studio20完美汉化…

react路由懒加载lazy直接使用组件标签引发的问题?

文章 前言错误场景解决方案问题分析后言 前言 ✨他们是天生勇敢的开发者,我们创造bug,传播bug,毫不留情地消灭bug,在这个过程中我们创造了很多bug以供娱乐。 ✨ 这里是前端的一些bug 感兴趣的可以看看前端bug 错误场景 在react18t…

ios 代码上下文截屏之后导致的图片异常问题

业务场景,之前是直接将当前的collectionview截长屏操作,第一次截图会出现黑色部分原因是视图未完全布局,原因是第一次使用了Masonry约束然后再截图的时候进行了frame赋值,可以查看下Masonry约束和frame的冲突,全部修改…

高效学习工具之Anki新手入门指南(ios端,包括ipad、ihpone设备)————创建、使用、备份、设置参数、相关资料

文章目录 0 背景0.1 闭环学习0.2 什么是anki 1 开始使用1.1 导入1.2 创建空白组1.3 创建卡片1.3.1 利用anki创建卡片的两种方法1.3.2 复习材料分类 1.4 筛选(做减法)1.5 开启v3算法,设置排程 2 操作卡牌(位于卡牌界面中“游览”&a…

python采集电商jd app商品详情数据(2023-10-30)

一、技术要点: 1、cookie可以从手机app端用charles抓包获取; 2、无需安装nodejs,纯python源码; 3、商品详情接口为:functionId "wareBusiness"; 4、clientVersion "10.1.4"同…

微信小程序 人工智能志愿者服务活动报名系统uniAPP+vue

基于java语言设计并实现了人工智能志愿者服务APP。该APP基于B/S即所谓浏览器/服务器模式,应用SpringBoot框架与HBuilder X技术,选择MySQL作为后台数据库。系统主要包括用户、志愿活动、活动报名、活动签到、服务职责、服务排行等功能模块。 本文首先介绍…

canvas绘制签名并保存

实现签名的三个关键方法&#xff1a; 1.mousedown&#xff1a;当鼠标按下时开始绘制签名。 2.mousemove&#xff1a;鼠标移动时持续绘制。 3.mouseup&#xff1a;鼠标抬起时结束绘制。 html&#xff1a; <div class"setSign"><canvasref"canvas&q…

qt 系列(一)---qt designer设计常用操作

最近转战qt, 主要用qt designer 进行GUI开发&#xff0c;记录下实战经验~ 1.前言 qt 是跨平台C图形用户界面应用程序开发框架&#xff0c;可以使用的IDE工具有 qt creator 和 vs, 这里我主要使用 Visual Studio 2017 工具进行程序开发与编写。 2. 环境配置 只写关键步骤~~ …