Pytorch十九种损失函数的使用详解


Posted in Python onApril 29, 2020

损失函数通过torch.nn包实现,

1 基本用法

criterion = LossCriterion() #构造函数有自己的参数
loss = criterion(x, y) #调用标准时也有参数

2 损失函数

2-1 L1范数损失 L1Loss

计算 output 和 target 之差的绝对值。

torch.nn.L1Loss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-2 均方误差损失 MSELoss

计算 output 和 target 之差的均方差。

torch.nn.MSELoss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-3 交叉熵损失 CrossEntropyLoss

当训练有 C 个类别的分类问题时很有效. 可选参数 weight 必须是一个1维 Tensor, 权重将被分配给各个类别. 对于不平衡的训练集非常有效。

在多分类任务中,经常采用 softmax 激活函数+交叉熵损失函数,因为交叉熵描述了两个概率分布的差异,然而神经网络输出的是向量,并不是概率分布的形式。所以需要 softmax激活函数将一个向量进行“归一化”成概率分布的形式,再采用交叉熵损失函数计算 loss。

Pytorch十九种损失函数的使用详解

torch.nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction='mean')

参数:

weight (Tensor, optional) ? 自定义的每个类别的权重. 必须是一个长度为 C 的 Tensor
ignore_index (int, optional) ? 设置一个目标值, 该目标值会被忽略, 从而不会影响到 输入的梯度。
reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-4 KL 散度损失 KLDivLoss

计算 input 和 target 之间的 KL 散度。KL 散度可用于衡量不同的连续分布之间的距离, 在连续的输出分布的空间上(离散采样)上进行直接回归时 很有效.

torch.nn.KLDivLoss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-5 二进制交叉熵损失 BCELoss

二分类任务时的交叉熵计算函数。用于测量重构的误差, 例如自动编码机. 注意目标的值 t[i] 的范围为0到1之间.

torch.nn.BCELoss(weight=None, reduction='mean')

参数:

weight (Tensor, optional) ? 自定义的每个 batch 元素的 loss 的权重. 必须是一个长度为 “nbatch” 的 的 Tensor
pos_weight(Tensor, optional) ? 自定义的每个正样本的 loss 的权重. 必须是一个长度 为 “classes” 的 Tensor

2-6 BCEWithLogitsLoss

BCEWithLogitsLoss损失函数把 Sigmoid 层集成到了 BCELoss 类中. 该版比用一个简单的 Sigmoid 层和 BCELoss 在数值上更稳定, 因为把这两个操作合并为一个层之后, 可以利用 log-sum-exp 的 技巧来实现数值稳定.

torch.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)

参数:

weight (Tensor, optional) ? 自定义的每个 batch 元素的 loss 的权重. 必须是一个长度 为 “nbatch” 的 Tensor
pos_weight(Tensor, optional) ? 自定义的每个正样本的 loss 的权重. 必须是一个长度 为 “classes” 的 Tensor

2-7 MarginRankingLoss

torch.nn.MarginRankingLoss(margin=0.0, reduction='mean')

对于 mini-batch(小批量) 中每个实例的损失函数如下:

Pytorch十九种损失函数的使用详解

参数:

margin:默认值0

2-8 HingeEmbeddingLoss

torch.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')

对于 mini-batch(小批量) 中每个实例的损失函数如下:

Pytorch十九种损失函数的使用详解

参数:

margin:默认值1

2-9 多标签分类损失 MultiLabelMarginLoss

torch.nn.MultiLabelMarginLoss(reduction='mean')

对于mini-batch(小批量) 中的每个样本按如下公式计算损失:

Pytorch十九种损失函数的使用详解

2-10 平滑版L1损失 SmoothL1Loss

也被称为 Huber 损失函数。

torch.nn.SmoothL1Loss(reduction='mean')

Pytorch十九种损失函数的使用详解

其中

Pytorch十九种损失函数的使用详解

2-11 2分类的logistic损失 SoftMarginLoss

torch.nn.SoftMarginLoss(reduction='mean')

Pytorch十九种损失函数的使用详解

2-12 多标签 one-versus-all 损失 MultiLabelSoftMarginLoss

torch.nn.MultiLabelSoftMarginLoss(weight=None, reduction='mean')

Pytorch十九种损失函数的使用详解

2-13 cosine 损失 CosineEmbeddingLoss

torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')

Pytorch十九种损失函数的使用详解

参数:

margin:默认值0

2-14 多类别分类的hinge损失 MultiMarginLoss

torch.nn.MultiMarginLoss(p=1, margin=1.0, weight=None, reduction='mean')

Pytorch十九种损失函数的使用详解

参数:

p=1或者2 默认值:1
margin:默认值1

2-15 三元组损失 TripletMarginLoss

torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False, reduction='mean')

Pytorch十九种损失函数的使用详解

其中:

Pytorch十九种损失函数的使用详解

2-16 连接时序分类损失 CTCLoss

CTC连接时序分类损失,可以对没有对齐的数据进行自动对齐,主要用在没有事先对齐的序列化数据训练上。比如语音识别、ocr识别等等。

torch.nn.CTCLoss(blank=0, reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-17 负对数似然损失 NLLLoss

负对数似然损失. 用于训练 C 个类别的分类问题.

torch.nn.NLLLoss(weight=None, ignore_index=-100, reduction='mean')

参数:

weight (Tensor, optional) ? 自定义的每个类别的权重. 必须是一个长度为 C 的 Tensor
ignore_index (int, optional) ? 设置一个目标值, 该目标值会被忽略, 从而不会影响到 输入的梯度.

2-18 NLLLoss2d

对于图片输入的负对数似然损失. 它计算每个像素的负对数似然损失.

torch.nn.NLLLoss2d(weight=None, ignore_index=-100, reduction='mean')

参数:

weight (Tensor, optional) ? 自定义的每个类别的权重. 必须是一个长度为 C 的 Tensor
reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-19 PoissonNLLLoss

目标值为泊松分布的负对数似然损失

torch.nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction='mean')

参数:

log_input (bool, optional) ? 如果设置为 True , loss 将会按照公 式 exp(input) - target * input 来计算, 如果设置为 False , loss 将会按照 input - target * log(input+eps) 计算.
full (bool, optional) ? 是否计算全部的 loss, i. e. 加上 Stirling 近似项 target * log(target) - target + 0.5 * log(2 * pi * target).
eps (float, optional) ? 默认值: 1e-8

参考资料

pytorch loss function 总结

到此这篇关于Pytorch十九种损失函数的使用详解的文章就介绍到这了,更多相关Pytorch 损失函数内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python使用三角迭代计算圆周率PI的方法
Mar 20 Python
Python中的super用法详解
May 28 Python
python 时间戳与格式化时间的转化实现代码
Mar 23 Python
Tesserocr库的正确安装方式
Oct 19 Python
Python实现判断一个整数是否为回文数算法示例
Mar 02 Python
Flask框架单例模式实现方法详解
Jul 31 Python
Python 使用matplotlib模块模拟掷骰子
Aug 08 Python
Python实现投影法分割图像示例(二)
Jan 17 Python
python游戏开发的五个案例分享
Mar 09 Python
新版Pycharm中Matplotlib不会弹出独立的显示窗口的问题
Jun 02 Python
pycharm 关掉syntax检查操作
Jun 09 Python
Python实现自动签到脚本的示例代码
Aug 19 Python
Python格式化输出--%s,%d,%f的代码解析
Apr 29 #Python
Python爬虫工具requests-html使用解析
Apr 29 #Python
Python基于Hypothesis测试库生成测试数据
Apr 29 #Python
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
Apr 29 #Python
Python通过两个dataframe用for循环求笛卡尔积
Apr 29 #Python
Django分组聚合查询实例分享
Apr 29 #Python
python中sympy库求常微分方程的用法
Apr 28 #Python
You might like
PHP脚本数据库功能详解(上)
2006/10/09 PHP
php的curl实现get和post的代码
2008/08/23 PHP
浅谈php错误提示及查错方法
2015/07/14 PHP
PHP封装的多文件上传类实例与用法详解
2017/02/07 PHP
怎么用javascript进行拖拽
2006/07/20 Javascript
jquery animate 动画效果使用说明
2009/11/04 Javascript
JavaScript高级程序设计(第3版)学习笔记13 ECMAScript5新特性
2012/10/11 Javascript
Javascript核心读书有感之类型、值和变量
2015/02/11 Javascript
js性能优化技巧
2015/11/29 Javascript
JS去掉字符串前后空格或去掉所有空格的用法
2017/03/25 Javascript
jQuery实现简单的滑动导航代码(移动端)
2017/05/22 jQuery
this.$toast() 了解一下?
2019/04/18 Javascript
jQuery使用ajax传递json对象到服务端及contentType的用法示例
2020/03/12 jQuery
[52:44]VGJ.T vs infamous Supermajor小组赛D组败者组第一轮 BO3 第一场 6.3
2018/06/04 DOTA
python处理数据,存进hive表的方法
2018/07/04 Python
Python清空文件并替换内容的实例
2018/10/22 Python
python使用ddt过程中遇到的问题及解决方案【推荐】
2018/10/29 Python
Python WEB应用部署的实现方法
2019/01/02 Python
Python pandas用法最全整理
2019/08/04 Python
python实现图片上添加图片
2019/11/26 Python
BeautifulSoup获取指定class样式的div的实现
2020/12/07 Python
纯css3实现思维导图样式示例
2018/11/01 HTML / CSS
CSS3 @media的基本用法总结
2019/09/10 HTML / CSS
西海岸男士和男童服装:Johnnie-O
2018/03/15 全球购物
澳大利亚100%丝绸多彩度假装商店:TheSwankStore
2019/09/04 全球购物
说一下Linux下有关用户和组管理的命令
2014/08/18 面试题
北体毕业生求职信
2014/02/28 职场文书
学校工作推荐信范文
2014/07/11 职场文书
婚前协议书标准版
2014/10/19 职场文书
全国法制宣传日活动总结2014
2014/11/01 职场文书
先进单位申报材料
2014/12/25 职场文书
2015年保险业务员工作总结
2015/05/27 职场文书
使用PostGIS完成两点间的河流轨迹及流经长度的计算(推荐)
2022/01/18 PostgreSQL
MySQL的prepare使用以及遇到的bug
2022/05/11 MySQL
win sever 2022如何占用操作主机角色
2022/06/25 Servers
Win10服务全部禁用了怎么启动?Win10服务全部禁用解决方法
2022/09/23 数码科技