当前位置:网站首页>损失函数~
损失函数~
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边栏推荐
- Storage unit conversion
- Jerry's fast touch does not respond [chapter]
- Using rendertext() to output multiple lines of text with rendertext() in R shiny
- Jielizhi, production line assembly link [chapter]
- [chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
- PMP project integration management
- Baidu AI Cloud - create a face recognition application
- 位的高阶运算
- 地方经销商玩转社区团购模式,百万运营分享
- [leetcode] most elements [169]
猜你喜欢

Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly

海思3559万能平台搭建:在截获的YUV图像上画框

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

电商系统微服务架构
![[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
![[LeetCode] 反转字符串中的单词 III【557】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 反转字符串中的单词 III【557】

wait解决僵尸进程

Hanging mirror security won four global infosec awards on rsac2022
![[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')](/img/02/67448df1817e8b34b654722df8ecd4.jpg)
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')

从2022年Q1财报看携程的韧性和远景
随机推荐
Uniapp wechat login returns user name and Avatar
easyclick,ec权朗网络验证源码
Local dealers play the community group purchase mode and share millions of operations
Splunk audit 的设定
解决Chrome浏览器和Edeg浏览器主页被篡改的方法
Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
[LeetCode] 反转字符串中的单词 III【557】
牛客网:龙与地下城游戏
LeetCode 968. 监控二叉树
Stop slave is stuck -- the event of the transaction is not copied completely
送给即将工作的自己
Graphic view frame
Jerry's prototype will trigger shutdown after multiple touches [chapter]
用sentinel熔断比例阈值改不了,设置慢调用比例没效果
移动端 1px 像素兼容性问题,实现1px 边框
Jerry's fast touch does not respond [chapter]
分布式监控系统zabbix
P7072 [csp-j2020] live broadcast Award
MySQL reset password, forget password, reset root password, reset MySQL password
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?

