pytorch 交叉熵损失函数 BCELoss

BCE Loss

交叉熵损失函数计算公式:

BCE Loss = - 1/n*(y_actual * log(y_pred) + (1 - y_actual) * log(1 - y_pred))

t[i]为标签值:0或者1

o[i]是经过sigmoid后的概率值

BCEWithLogitsLoss

这个损失将Sigmoid层和BCELoss合并在一个类中。

BCEWithLogitsLoss`(weight=None, size_average=None, reduce=None, reduction='mean', pos_weight=None)

python 复制代码
import torch
from torch import autograd
input = autograd.Variable(torch.tensor([[ 1.9072,  1.1079,  1.4906],
        [-0.6584, -0.0512,  0.7608],
        [-0.0614,  0.6583,  0.1095]]), requires_grad=True)
print(input)
print('-'*100)

from torch import nn
m = nn.Sigmoid()
print(m(input))
print('-'*100)

target = torch.FloatTensor([[0, 1, 1], [1, 1, 1], [0, 0, 0]])
print(target)
print('-'*100)

import math

r11 = 0 * math.log(0.8707) + (1-0) * math.log((1 - 0.8707))
r12 = 1 * math.log(0.7517) + (1-1) * math.log((1 - 0.7517))
r13 = 1 * math.log(0.8162) + (1-1) * math.log((1 - 0.8162))

r21 = 1 * math.log(0.3411) + (1-1) * math.log((1 - 0.3411))
r22 = 1 * math.log(0.4872) + (1-1) * math.log((1 - 0.4872))
r23 = 1 * math.log(0.6815) + (1-1) * math.log((1 - 0.6815))

r31 = 0 * math.log(0.4847) + (1-0) * math.log((1 - 0.4847))
r32 = 0 * math.log(0.6589) + (1-0) * math.log((1 - 0.6589))
r33 = 0 * math.log(0.5273) + (1-0) * math.log((1 - 0.5273))

r1 = -(r11 + r12 + r13) / 3
#0.8447112733378236
r2 = -(r21 + r22 + r23) / 3
#0.7260397266631787
r3 = -(r31 + r32 + r33) / 3
#0.8292933181294807
bceloss = (r1 + r2 + r3) / 3 
print(bceloss)
print('-'*100)

loss = nn.BCELoss()
print(loss(m(input), target))
print('-'*100)

loss = nn.BCEWithLogitsLoss()
print(loss(input, target))

结果

原始的3x3矩阵:

tensor([[ 1.9072, 1.1079, 1.4906],

-0.6584, -0.0512, 0.7608\], \[-0.0614, 0.6583, 0.1095\]\], requires_grad=True) ---------------------------------------------------------------------------------------------------- ### 使用Sigmoid矩阵进行计算: tensor(\[\[0.8707, 0.7517, 0.8162\], \[0.3411, 0.4872, 0.6815\], \[0.4847, 0.6589, 0.5273\]\], grad_fn=\) ---------------------------------------------------------------------------------------------------- ### 二分类标签: tensor(\[\[0., 1., 1.\], \[1., 1., 1.\], \[0., 0., 0.\]\]) ---------------------------------------------------------------------------------------------------- ### 手动计算的结果: 0.8000147727101611 ---------------------------------------------------------------------------------------------------- ### 使用BCE Loss对sigmoid后的计算的结果: tensor(0.8000, grad_fn=\) ---------------------------------------------------------------------------------------------------- ### 使用BCEWithLogitsLoss直接对原始数据计算的结果: tensor(0.8000, grad_fn=\)

相关推荐
CoovallyAIHub16 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
CoovallyAIHub16 小时前
开源的消逝与新生:从 TensorFlow 的落幕到开源生态的蜕变
pytorch·深度学习·llm
CoovallyAIHub1 天前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
惯导马工2 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
隐语SecretFlow3 天前
国人自研开源隐私计算框架SecretFlow,深度拆解框架及使用【开发者必看】
深度学习
Billy_Zuo3 天前
人工智能深度学习——卷积神经网络(CNN)
人工智能·深度学习·cnn
羊羊小栈3 天前
基于「YOLO目标检测 + 多模态AI分析」的遥感影像目标检测分析系统(vue+flask+数据集+模型训练)
人工智能·深度学习·yolo·目标检测·毕业设计·大作业
l12345sy3 天前
Day24_【深度学习—广播机制】
人工智能·pytorch·深度学习·广播机制
九章云极AladdinEdu3 天前
超参数自动化调优指南:Optuna vs. Ray Tune 对比评测
运维·人工智能·深度学习·ai·自动化·gpu算力