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

相关推荐
程序员打怪兽21 小时前
详解Visual Transformer (ViT)网络模型
深度学习
CoovallyAIHub3 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub3 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub3 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub3 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub3 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
用户1474853079744 天前
AI-动手深度学习环境搭建-d2l
深度学习
OpenBayes贝式计算4 天前
解决视频模型痛点,TurboDiffusion 高效视频扩散生成系统;Google Streetview 涵盖多个国家的街景图像数据集
人工智能·深度学习·机器学习
OpenBayes贝式计算4 天前
OCR教程汇总丨DeepSeek/百度飞桨/华中科大等开源创新技术,实现OCR高精度、本地化部署
人工智能·深度学习·机器学习
在人间耕耘5 天前
HarmonyOS Vision Kit 视觉AI实战:把官方 Demo 改造成一套能长期复用的组件库
人工智能·深度学习·harmonyos