当前位置:网站首页>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
边栏推荐
猜你喜欢

Ctf-misc learning from start to give up

Série de pénétration Intranet: icmpsh du tunnel Intranet

Research on software security based on NLP (2)

Guoji Beisheng openstack container cloud environment construction

Internal network security attack and defense: a practical guide to penetration testing (8): Authority maintenance analysis and defense

Unity get real geographic map application terrain notes

Essays (updated from time to time)

内网渗透系列:内网隧道之icmpsh

Intranet penetration series: dns2tcp of Intranet tunnel

C problem of marking the position of polygons surrounded by multiple rectangles
随机推荐
Intranet penetration series: icmptunnel of Intranet tunnel (by master dhavalkapil)
Sto with billing cross company inventory dump return
KVM安装部署
【编程实践/嵌入式比赛】嵌入式比赛学习记录(一):TCP服务器和web界面的建立
[NLP notes] preliminary study on CRF principle
The displayed amount of ABAP ALV is inconsistent with the exported amount
NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
BUUCTF MISC刷题
内网渗透系列:内网隧道之dnscat2
linux下mysql数据库备份与恢复(全量+增量)
C read INI file and write data to INI file
Intranet penetration series: ICMP of Intranet tunnel_ Tran
Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)
内网渗透系列:内网隧道之icmptunnel(jamesbarlow师傅的)
Alibaba sentinel学习QA
内网渗透系列:内网隧道之icmptunnel(DhavalKapil师傅的)
MySQL--锁的奥秘--数据怎么锁
【编程实践/嵌入式比赛】嵌入式比赛学习记录(二):基于TCP的图片流传输
Chapter VII asset impairment
Unity C single case mode learning review notes