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

相关推荐
童话名剑5 小时前
人脸识别(吴恩达深度学习笔记)
人工智能·深度学习·人脸识别·siamese网络·三元组损失函数
Dev7z6 小时前
基于深度学习的车辆分类方法研究与实现-填补国内新能源车型和品牌识别空白
深度学习·yolo
谢的2元王国6 小时前
这是跑通实用rag的日志记录 重点关注一点 句子向量化模型的选择 以及召回结果后 重排交叉编码的精进
人工智能·深度学习
Coco恺撒7 小时前
【脑机接口+人工智能】阔别三载,温暖归来
人工智能·经验分享·神经网络·人机交互·创业创新·学习方法
Blossom.1187 小时前
AI Agent的长期记忆革命:基于向量遗忘曲线的动态压缩系统
运维·人工智能·python·深度学习·自动化·prompt·知识图谱
岑梓铭7 小时前
YOLO深度学习(计算机视觉)—毕设笔记1(介绍篇)
深度学习·yolo·目标检测·计算机视觉
ldccorpora8 小时前
GALE Phase 1 Chinese Broadcast News Parallel Text - Part 1数据集介绍,官网编号LDC2007T23
人工智能·深度学习·算法·机器学习·自然语言处理
AI人工智能+9 小时前
表格识别技术:实现复杂表格内容的精准解析与表格结构的版面还原,推动档案管理从数字化存储向智能化服务转型
深度学习·ocr·表格识别
Hcoco_me10 小时前
大模型面试题75:讲解一下GRPO的数据回放
人工智能·深度学习·算法·机器学习·vllm
高洁0111 小时前
AIGC技术与进展(1)
深度学习·算法·机器学习·transformer·知识图谱