当前位置:网站首页>损失函数~
损失函数~
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
边栏推荐
猜你喜欢
随机推荐
'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
Webrtc audio and video capture and playback examples and mediastream media stream analysis
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
Introduction and response to high concurrency
E-commerce system microservice architecture
Qt QScrollArea
地平线2022年4月最新方案介绍
Go four singleton modes
easyclick,ec权朗网络验证源码
Jerry's prototype will trigger shutdown after multiple touches [chapter]
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
严守工期,确保质量,这家AI数据标注公司做到了!
中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!
Mask R-CNN
杰理之、产线装配环节【篇】
[LeetCode] 多数元素【169】
go 条件变量
Using rendertext() to output multiple lines of text with rendertext() in R shiny