当前位置:网站首页>损失函数~
损失函数~
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
二分类用的交叉熵,其计算公式较复杂,这里主要是有个概念即可,一般情况下不会用到。
criterion = nn.BCELoss()
loss = criterion(sample, target)
print(loss)
# -13.8155
边栏推荐
- Kubernetes uses the host name to allocate the pod on the specified node
- JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
- Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
- E-commerce system microservice architecture
- Local dealers play the community group purchase mode and share millions of operations
- 牛客网:最大子矩阵
- [Solved] Splunk: Cannot get username when all users are selected“
- Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- 小鹏P7出事故,安全气囊未弹出,这正常吗?
猜你喜欢
随机推荐
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
杰理之如何测试按键的误触率【篇】
分布式监控系统zabbix
Jerry's charge unplugged, unable to touch the boot [chapter]
Jatpack------LiveData
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
P7072 [CSP-J2020] 直播获奖
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
[leetcode] there are duplicate elements [217]
[LeetCode] 反转字符串【344】
Qt QProgressBar详解
容器化技术在嵌入式领域的应用
杰理之样机在多次触摸后会触发关机【篇】
Webrtc audio and video capture and playback examples and mediastream media stream analysis
Higher order operation of bits
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
数组进阶提高
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
How does Jerry test the wrong touch rate of keys [chapter]