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有很大区别**

相关推荐
高洁0133 分钟前
具身智能-8家国内外典型具身智能VLA模型深度解析
深度学习·神经网络·aigc·transformer·知识图谱
李昊哲小课1 小时前
wsl ubuntu24.04 cuda13 cudnn9 pytorch 显卡加速
人工智能·pytorch·python·cuda·cudnn
钅日 勿 XiName3 小时前
一小时速通Pytorch之神经网络相关知识(三)
人工智能·pytorch·神经网络
shayudiandian5 小时前
Transformer结构完全解读:从Attention到LLM
人工智能·深度学习·transformer
xier_ran5 小时前
深度学习:深入理解 Softmax 激活函数
人工智能·深度学习
aitoolhub6 小时前
重塑机器人未来:空间智能驱动产业智能化升级
大数据·人工智能·深度学习·机器学习·机器人·aigc
淬炼之火6 小时前
阅读:基于深度学习的红外可见光图像融合综述
图像处理·深度学习·机器学习·计算机视觉·特征融合·红外图像识别
bst@微胖子7 小时前
ModelScope微调模型
人工智能·深度学习·bert
xier_ran7 小时前
深度学习:动量梯度下降实战(Momentum Gradient Descent)
人工智能·深度学习