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内置数据类型详解
Aug 18 Python
Python实现telnet服务器的方法
Jul 10 Python
在Django中同时使用多个配置文件的方法
Jul 22 Python
完美解决python遍历删除字典里值为空的元素报错问题
Sep 11 Python
python3 cvs将数据读取为字典的方法
Dec 22 Python
Python实现查找数组中任意第k大的数字算法示例
Jan 23 Python
Python应用实现处理excel数据过程解析
Jun 19 Python
Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法
Jul 28 Python
详解python变量与数据类型
Aug 25 Python
python实现录音功能(可随时停止录音)
Oct 26 Python
Python的Tqdm模块实现进度条配置
Feb 24 Python
代码复现python目标检测yolo3详解预测
May 06 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
在Windows中安装Apache2和PHP4的权威指南
2006/10/09 PHP
php实现约瑟夫问题的方法小结
2015/03/23 PHP
实现laravel 插入操作日志到数据库的方法
2019/10/11 PHP
PHP pthreads v3使用中的一些坑和注意点分析
2020/02/21 PHP
javascript 用原型继承来实现对象系统
2010/03/22 Javascript
jQuery.clean使用方法及思路分析
2013/01/07 Javascript
javaScript NameSpace 简单说明介绍
2013/07/18 Javascript
从jquery的过滤器.filter()方法想到的
2013/09/29 Javascript
jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
2017/06/26 jQuery
详解vue+webpack+express中间件接口使用
2018/07/17 Javascript
vue 对象添加或删除成员时无法实时更新的解决方法
2019/05/01 Javascript
vue进入页面时不在顶部,检测滚动返回顶部按钮问题及解决方法
2019/10/30 Javascript
python实现迭代法求方程组的根过程解析
2019/11/25 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛 DT VS HGT
2014/05/22 DOTA
[05:45]Ti4观战指南(下)
2014/07/07 DOTA
python 七种邮件内容发送方法实例
2014/04/22 Python
Python文件夹与文件的操作实现代码
2014/07/13 Python
Python爬取读者并制作成PDF
2015/03/10 Python
基于Django的python验证码(实例讲解)
2017/10/23 Python
python随机数分布random测试
2018/08/27 Python
kafka-python 获取topic lag值方式
2019/12/23 Python
解决pyinstaller 打包exe文件太大,用pipenv 缩小exe的问题
2020/07/13 Python
详解python爬取弹幕与数据分析
2020/11/14 Python
锐步美国官方网站:Reebok美国
2018/01/10 全球购物
比利时的在线灯具店:Lampen24.be
2019/07/01 全球购物
波兰最大的宠物用品网上商店:FERA.PL
2019/08/11 全球购物
英国现代、当代和设计师家具店:Furntastic
2020/07/18 全球购物
《一本男孩子必读的书》教学反思
2014/02/19 职场文书
银行职员自我鉴定
2014/04/20 职场文书
给校长的建议书300字
2014/05/16 职场文书
庆元旦活动总结
2014/07/09 职场文书
北大自主招生自荐信
2015/03/04 职场文书
客服专员岗位职责范本
2015/04/07 职场文书
2015年公务员个人工作总结
2015/04/24 职场文书
HTML常用标签超详细整理
2022/03/19 HTML / CSS
Mysql分析设计表主键为何不用uuid
2022/03/31 MySQL