当前位置:网站首页>NLLLoss+log_SoftMax=CE_Loss
NLLLoss+log_SoftMax=CE_Loss
2022-04-23 06:34:00 【365JHWZGo】
题目
NLLLoss+log_SoftMax=CE_Loss
前言
差不多好几天没有更新了,唉,最近有点忙,请见谅!今天终于忙里偷闲看了B站视频发现NLLLoss+log_SoftMax=CE_Loss,哈哈,又长见识了,那么今天来深入了解一下吧!
NLLLoss
中文
负对数似然损失
函数
torch.nn.NLLLoss(
weight=None,
size_average=None,
ignore_index=- 100,
reduce=None,
reduction='mean'
)
参数详解
参数 | 类型 | 含义 |
---|---|---|
weight | tensor(optional) | 手动给每一个类重新调整权重比例 |
size_average | bool(optional)已弃用 | 损失是一个batch中每一个损失元素的平均值 |
ignore_index | int(optional) | 忽略一个目标值后它不会对输入的梯度产生影响 |
reduce | bool(optional)已弃用 | 根据每个mini-batch的平均尺寸对损失进行均分或汇总 |
reduction | string(optional) | reuduction将会被应用于输出 |
reduction参数 | 解释 |
---|---|
none | 全部展开 |
mean | 累加/个数 |
sum | 把none结果累加 |
函数输入输出
输入 | 目标 | 输出 | |
---|---|---|---|
类型 | tensor | tensor | tensor |
维度 | ( N , C N,C N,C)/( C C C) | ( N N N)/() | ( N N N)/ ( N , d 1 , d 2 , . . . , d K ) (N, d_1, d_2, ..., d_K) (N,d1,d2,...,dK) |
代码
import torch
# 模仿经过模型之后的输出结果
preds = torch.tensor([[1.5,2.5,3.0]])
# 真实标签
target = torch.tensor([1])
nllloss = torch.nn.NLLLoss()
print('nllloss:',nllloss(preds,target))
与CE_Loss的区别和联系
区别:
- CE_Loss是先经过log_softmax再经过NLLLoss步骤的损失
- NLLLoss仅仅是将对应index的target值变为相反数
联系:
- 存在着一定的相关性,经过softmax之后的值大都在[0,1]之间,在进行log取对数之后值分布在( − ∞ -\infty −∞,0]之间,而loss又不能为负,所以需要NLLLoss来将其变为正数
import torch
# 模仿经过模型之后的输出结果
preds = torch.tensor([[1.5,2.5,3.0]])
# 真实标签
target = torch.tensor([1])
cross_entropy_loss = torch.nn.CrossEntropyLoss()
log_softmax = torch.nn.LogSoftmax(dim=1)
nllloss = torch.nn.NLLLoss()
cs_loss = cross_entropy_loss(preds,target)
nls_loss = nllloss(log_softmax(preds),target)
print(f'交叉熵损失函数为:{
cs_loss}\n先经过log_softmax再经过nll损失函数为:{
nls_loss}')
版权声明
本文为[365JHWZGo]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44833392/article/details/124342281
边栏推荐
- [NLP notes] preliminary study on CRF principle
- Interview learning route
- 读书笔记
- Intranet security attack and defense: a practical guide to penetration testing (6): domain controller security
- GUI,CLI与Unix哲学
- Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)
- 攻防世界MISC刷题1-50
- FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
- Ctf-misc summary
- 数据库之Mysql——概述安装篇
猜你喜欢
Mysql database backup and recovery under Linux (full + incremental)
Analysis of Nacos source code
CTF攻防世界刷题51-
SAP STO With Billing流程与配置
Série de pénétration Intranet: icmpsh du tunnel Intranet
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
Simplify exporting to SVG data files and all images in SVG folder
利用sqlmap注入获取网址管理员账号密码
Intranet penetration series: icmptunnel of Intranet tunnel (Master James Barlow's)
When using flash, the code ends automatically without an error, the connection cannot be maintained, and the URL cannot be accessed.
随机推荐
DVWA靶场练习
How does Apache Hudi accelerate traditional batch mode?
三分钟教你用Houdini流体>>解算粒子流体水滴
Research on system and software security (I)
SAP STO With Billing流程与配置
读书笔记
Chapter V investment real estate
A series of articles, a summary of common vulnerabilities of Web penetration (continuously updated)
内网渗透系列:内网隧道之icmpsh
C problem of marking the position of polygons surrounded by multiple rectangles
KVM安装部署
Intranet penetration series: icmptunnel of Intranet tunnel (by master dhavalkapil)
SAP self created table log function is enabled
nacos源码分析思路
SAP GUI安全性
Houdini地形与流体解算(模拟泥石流)
Houdini fluid > > particle fluid export to unity note
攻防世界MISC刷题1-50
sentinel集成nacos动态更新数据原理
[极客大挑战 2019]Havefun1