PyTorch中CrossEntropyLoss、BCELoss、BCEWithLogitsLoss的理解

import torch

predict =torch.Tensor([[0.5796,0.4403,0.9087],[-1.5673,-0.3150,1.6660]])

#predict =torch.Tensor([[0.5796,0.4403],[-1.5673,-0.3150]])

print(predict)

target =torch.tensor([0,2])

target_bce =torch.Tensor([[1,0,0],[0,0,1]])

ce_loss=torch.nn.CrossEntropyLoss()

soft_max=torch.nn.Softmax(dim=-1)

sig_max=torch.nn.Sigmoid()

soft_out=soft_max(predict)

sig_out=sig_max(predict)

bce_loss=torch.nn.BCELoss()

bce_loss1=torch.nn.BCEWithLogitsLoss()

print(ce_loss(predict,target))

print(bce_loss(soft_out,target_bce))

print(bce_loss(sig_out,target_bce))

print(bce_loss1(predict,target_bce))

输出:

#predict:

**tensor([[ 0.5796, 0.4403, 0.9087],

-1.5673, -0.3150, 1.6660\]\])** #print(ce_loss(predict,target)): **tensor(0.6725)** #print(bce_loss(soft_out,target_bce)) **tensor(0.3950)** #print(bce_loss(sig_out,target_bce)) **tensor(0.5900)** print(bce_loss1(predict,target_bce)) **tensor(0.5900)** **结论:** **1.sigmoid激活+BCELoss等于BCEWithLogitsLoss** **2.BCEWithLogitsLoss和CrossEntropyLoss不一样,但都可以不加激活** **3.sigmoid激活+BCELoss和softmax激活+BCELoss有很大区别**

相关推荐
这张生成的图像能检测吗4 小时前
(论文速读)WFF-Net:用于表面缺陷检测的可训练权重特征融合卷积神经网络
人工智能·深度学习·神经网络·缺陷检测·图像分割
jz_ddk5 小时前
[算法] 算法PK:LMS与RLS的对比研究
人工智能·神经网络·算法·信号处理·lms·rls·自适应滤波
jay神5 小时前
【原创】基于YOLO模型的手势识别系统
深度学习·yolo·计算机·毕业设计·软件设计与开发
今天吃饺子5 小时前
如何用MATLAB调用python实现深度学习?
开发语言·人工智能·python·深度学习·matlab
破烂pan6 小时前
lmdeploy.pytorch 新模型支持代码修改
python·深度学习·llm·lmdeploy
嵌入式-老费6 小时前
自己动手写深度学习框架(pytorch入门)
人工智能·pytorch·深度学习
汗流浃背了吧,老弟!7 小时前
预训练语言模型(Pre-trained Language Model, PLM)介绍
深度学习·语言模型·自然语言处理
化作星辰7 小时前
深度学习_神经网络激活函数
人工智能·深度学习·神经网络
木头左13 小时前
自适应门控循环单元GRU-O与标准LSTM在量化交易策略中的性能对比实验
深度学习·gru·lstm
哥布林学者13 小时前
吴恩达深度学习课程二: 改善深层神经网络 第三周:超参数调整,批量标准化和编程框架(三)多值预测与多分类
深度学习·ai