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

相关推荐
羑悻的小杀马特1 小时前
从神经架构到万物自动化的 AI 革命:解码深度学习驱动的智能自动化新范式
深度学习·自动化
liuyang-neu2 小时前
目标检测DN-DETR(2022)详细解读
人工智能·深度学习·目标检测
AndrewHZ3 小时前
【ISP算法精粹】什么是global tone mapping和local tone mapping?
人工智能·深度学习·算法·计算机视觉·视觉算法·isp算法·色调映射
熊猫在哪4 小时前
野火鲁班猫(arrch64架构debian)从零实现用MobileFaceNet算法进行实时人脸识别(三)用yolov5-face算法实现人脸检测
人工智能·python·嵌入式硬件·神经网络·yolo·目标检测·机器学习
AIGC方案4 小时前
人工智能、深度学习、机器学习的联系与区别
人工智能·深度学习·机器学习
deephub5 小时前
5个开源MCP服务器:扩展AI助手能力,高效处理日常工作
人工智能·深度学习·大语言模型·mcp
乌旭5 小时前
去中心化算力池:基于IPFS+智能合约的跨校GPU资源共享平台设计
人工智能·深度学习·架构·去中心化·区块链·智能合约·risc-v
三道杠卷胡7 小时前
【AI News | 20250520】每日AI进展
人工智能·pytorch·python·语言模型·github
九章云极AladdinEdu8 小时前
深度学习框架显存泄漏诊断手册(基于PyTorch的Memory Snapshot对比分析方法)
人工智能·pytorch·python·深度学习·机器学习·transformer·gpu算力
FL16238631299 小时前
咖啡叶子病害检测数据集VOC+YOLO格式1468张4类别均为单叶子
深度学习·yolo·机器学习