当前位置:网站首页>nn. BCEWithLogisticLoss() & nn. The difference between bceloss(), nn CrossEntropyLoss() & nn. Nllloss() differences
nn. BCEWithLogisticLoss() & nn. The difference between bceloss(), nn CrossEntropyLoss() & nn. Nllloss() differences
2022-07-18 18:23:00 【jq_ ninety-eight】
nn.BCEWithLogisticLoss() = F.sigmoid() + F.binary_cross_entropy()/nn.BCELoss()
import torch
import torch.nn as nn
import torch.nn.functional as F
""" nn.BCEWithLogisticLoss() = F.sigmoid() + F.binary_cross_entropy()/nn.BCELoss() """
pred = torch.tensor([[-0.2],[0.2],[0.8]])
target = torch.FloatTensor([[0],[0],[1]])
bce_loss = F.binary_cross_entropy(F.sigmoid(pred),target)
# sigmoid = nn.Sigmoid()
# loss_bce = nn.BCELoss()
# bce_loss = loss_bce(sigmoid(pred),target)
print('bce_out',bce_loss)
bcew_loss = F.binary_cross_entropy_with_logits(pred,target)
# loss_bcewlogistic = nn.BCEWithLogitsLoss()
# bcew_loss = loss_bcewlogistic(pred, target)
print('bcewlogistic_out',bcew_loss )
# output
bce_out tensor(0.5891)
bcewlogistic_out tensor(0.5891)
nn.CrossEntropyLoss = F.log_softmax() + F.nll_loss()/nn.NLLLoss()
import torch
import torch.nn as nn
import torch.nn.functional as F
""" nn.CrossEntropyLoss = F.log_softmax() + F.nll_loss()/nn.NLLLoss() """
data = torch.randn(2, 5)
nn.Softmax()
log_soft = F.log_softmax(data, dim=1)
print('log_soft:', log_soft, '\n')
target = torch.tensor([1, 2])
# Use F.cross_entropy()
# entropy_out = F.cross_entropy(data, target)
EntropyLoss = nn.CrossEntropyLoss()
entropy_out = EntropyLoss(data, target)
# Use F.nll_loss()
# nll_out = F.nll_loss(log_soft, target)
# Use nn.NLLLoss()
NLLLoss = nn.NLLLoss() # nn.NLLLoss() Need to instantiate
nll_out = NLLLoss(log_soft, target)
print('entropy_out:', entropy_out)
print('nll_out:', nll_out)
log_soft: tensor([[-3.1368, -2.0374, -1.2469, -1.2996, -1.3236],
[-1.2834, -1.0692, -2.5731, -1.5507, -2.3944]])
entropy_out: tensor(2.3053)
nll_out: tensor(2.3053)
边栏推荐
- S7-200SMART案例分析——运动控制编程(二)
- 2022 P gas cylinder filling examination questions and simulation examination
- July training (day 16) - queue
- 数据可视化之matplotlib绘制正余弦曲线图
- 阿里妈妈展示广告引擎新探索:迈向全局最优算力分配
- pycuda 安装完毕,验证步骤
- NoSQLAttack工具下载与使用
- 【golang】cannot unmarshal xxx “ into Go struct field xxx of type xxx
- 【产品人卫朋】2022年产品人必备的13个设计类网站(1.0版)
- 北京华联BHG Mall持续发力,BHG DAY引领城市消费新热潮
猜你喜欢

module ‘sklearn. datasets‘ has no attribute ‘fetch_ mldata‘

【golang】cannot unmarshal xxx “ into Go struct field xxx of type xxx

What should I do if I can't see any tiles on SAP Fiori launchpad?

ASP.NET印刷行业印务管理系统,源码免费分享

监督学习week 3: Logistic Regression optional_lab收获记录

10 first and 2 second, the NLP team of Dharma hall won the championship in semeval 2022

Huawei image xmage: seek all the images in the world, and finally see the Bodhi Heart

module ‘sklearn.datasets‘ has no attribute ‘fetch_mldata‘

华为影像XMAGE:求尽世间像,终见菩提心

(pytorch进阶之路四)Vision Transformer
随机推荐
華為影像XMAGE:求盡世間像,終見菩提心
暑期学习Matlab笔记
What should I do if I can't see any tiles on SAP Fiori launchpad?
(manual) [sqli labs46, 47] order by injection, error echo, post injection, number / character type
Thread.sleep简介说明
质量与效率并重,测试左移助力块存储技术研发
Lime of Py: a detailed introduction to the introduction, installation and use of the lime Library
请查收,您有一份阿里先锋开源项目清单
10 first and 2 second, the NLP team of Dharma hall won the championship in semeval 2022
【C#】正序、逆序、最大值、最小值和平均值
Introduction to replacement technology of SAP ABAP CDs view view
2022 G3 boiler water treatment examination question bank and simulation examination
Method to delete all child nodes of an element
(pytorch进阶之路四)Vision Transformer
module ‘sklearn.datasets‘ has no attribute ‘fetch_mldata‘
Unity游戏文件大,如何缩小游戏文件
[CVPR2019] On Stabilizing Generative Adversarial Training with Noise
C#网络应用编程,实验一:WPF练习
【Renesas RA6M4开发板之I2C读取BMP180气压温度】
【產品人衛朋】2022年產品人必備的13個設計類網站(1.0版)