当前位置:网站首页>损失函数~
损失函数~
2022-07-02 22:09:00 【沉沉沉小姐】
概念:
损失函数是指用于计算标签值和预测值之间差异的函数,在机器学习过程中,有多种损失函数可供选择,典型的有距离向量,绝对向量等。

上图是一个用来模拟线性方程自动学习的示意图。粗线是真实的线性方程,虚线是迭代过程的示意,w1是第一次迭代的权重,w2是第二次迭代的权重,w3是第三次迭代的权重。随着迭代次数的增加,我们的目标是使得wn无限接近真实值。
图中1/2/3这三个标签分别是3次迭代过程中预测Y值和真实Y值之间的差值(这里差值就是损失函数的意思了,当然了,实际应用中存在多种差值计算的公式),这里的差值示意图上是用绝对差来表示的。在多维空间中还有平方差,均方差等多种不同的距离计算公式,也就是损失函数了。
常见损失函数的计算方法:
1. nn.L1Loss损失函数

L1Loss计算方法很简单,取预测值和真实值的绝对误差的平均数即可。
criterion = nn.L1Loss()
loss = criterion(sample, target)
print(loss)
# 1最后输出结果为1
计算逻辑如为:
- 先计算绝对差总和:|0-1|+|1-1|+|2-1|+|3-1| = 4
- 然后再平均:4/4 = 1
2. nn.SmoothL1Loss
SmoothL1Loss也叫作Huber Loss,误差在(-1,1)上是平方损失,其他情况是L1损失。

criterion = nn.SmoothL1Loss()
loss = criterion(sample, target)
print(loss)
# 0.625最后输出结果为0.625
计算逻辑如为:
- 先计算绝对差总和:

- 然后再平均:2.5/4 = 0.625
3. nn.MSELoss
平方损失函数。其计算公式是预测值与真实值之间的平方和的平均数。

criterion = nn.MSELoss()
loss = criterion(sample, target)
print(loss)
# 1.5最后输出结果为1.5
计算逻辑如为:
- 先计算绝对差总和:

- 然后再平均:6/4 = 1.5
4. nn.BCELoss
二分类用的交叉熵,其计算公式较复杂,这里主要是有个概念即可,一般情况下不会用到。
![loss(o,t) = - \frac{1}{N}\sum_{i=1}^{N}\left [ t_i*log(o_i) + (1-t_i)*log(1-o_i) \right ]](http://img.inotgo.com/imagesLocal/202207/02/202207022103224504_1.gif)
criterion = nn.BCELoss()
loss = criterion(sample, target)
print(loss)
# -13.8155边栏推荐
- 地方经销商玩转社区团购模式,百万运营分享
- [chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
- 最小生成树 Minimum Spanning Tree
- [LeetCode] 存在重复元素【217】
- 景联文科技低价策略帮助AI企业降低模型训练成本
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- 泛型与反射,看这篇就够了
- [leetcode] reverse string [344]
- [leetcode] there are duplicate elements [217]
- Boot actuator - Prometheus use
猜你喜欢

AES高級加密協議的動機闡述

容器化技术在嵌入式领域的应用

电商系统微服务架构

海思3559万能平台搭建:在截获的YUV图像上旋转操作

中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!

數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
![[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?](/img/b5/e746dd115995e82c93f667c58a601c.png)
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
![[chestnut sugar GIS] ArcScene - how to make elevation map with height](/img/91/f3df0a7633263c6264cb5c27eb149f.png)
[chestnut sugar GIS] ArcScene - how to make elevation map with height

Qt QSplitter拆分器

Boot actuator - Prometheus use
随机推荐
[leetcode] number of palindromes [9]
Using rendertext() to output multiple lines of text with rendertext() in R shiny
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
To myself who is about to work
Jerry's charge unplugged, unable to touch the boot [chapter]
分布式监控系统zabbix
[LeetCode] 数组中的第K个最大元素【215】
go 多线程数据搜索
杰理之样机无触摸,拆机之后重新安装变正常【篇】
电路设计者常用的学习网站
世界环境日 | 周大福用心服务推动减碳环保
JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
P7072 [csp-j2020] live broadcast Award
Jatpack------LiveData
Webrtc audio and video capture and playback examples and mediastream media stream analysis
MySQL查询附近的数据.并按距离进行排序.
杰理之直接触摸样机的顶针反应不正常【篇】
性能优化----严苛模式
Learning records of data analysis (II) -- simple use of response surface method and design expert
全面解析分享购商业模式逻辑?分享购是如何赋能企业

