当前位置: 首页 > article >正文

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_)

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=<SigmoidBackward0>)
----------------------------------------------------------------------------------------------------

二分类标签:

tensor([[0., 1., 1.],
        [1., 1., 1.],
        [0., 0., 0.]])
----------------------------------------------------------------------------------------------------

手动计算的结果:

0.8000147727101611

----------------------------------------------------------------------------------------------------

使用BCE Loss对sigmoid后的计算的结果:

tensor(0.8000, grad_fn=<BinaryCrossEntropyBackward0>)
----------------------------------------------------------------------------------------------------

使用BCEWithLogitsLoss直接对原始数据计算的结果:

tensor(0.8000, grad_fn=<BinaryCrossEntropyWithLogitsBackward0>)


http://www.kler.cn/news/364468.html

相关文章:

  • ctfshow(41)--RCE/命令执行漏洞--或绕过
  • 使用 Python 和 Pandas 处理 Excel 数据:合并单元格示例
  • Android Room(SQLite) too many SQL variables异常
  • 【机器学习】VQ-VAE(Vector Quantized Variational Autoencoder)
  • GS-SLAM Dense Visual SLAM with 3D Gaussian Splatt 论文阅读
  • 解锁文本数据可视化的无限可能:Wordcloud库全解析
  • Java 代理模式详解
  • 003:无人机概述
  • 使用RabbitMQ实现延迟消息的完整指南
  • 瓜田推广:揭秘零撸项目里流量变现的技术与模式框架,新手必看!
  • EDA软件设计(1)----画板、侧边框和属性版的简易设计
  • 多模态技术串讲
  • CSDN介绍
  • Flink on yarn模式下,JobManager异常退出问题
  • 百度文心一言接入流程-java版
  • 前端全栈混合之路Deno篇:Deno2.0如何快速创建http一个 restfulapi/静态文件托管应用及oak框架介绍
  • 循环双链表,将L改造为L=(a1,a3,…,an,a4,a2)
  • 【Linux】实现一个简易的shell命令行
  • Vue3+ts+vite自动导入vue的依赖
  • 应用案例 | Panorama SCADA助力巴黎奥运会:保障赛事协调与安全
  • 【STM32 Blue Pill编程实例】-OLED显示BME280传感器数据
  • HTTP介绍及请求过程
  • 深度学习中的注意力机制:从基础到应用
  • Redis学习文档
  • BigFoot (Simplified Configuration)
  • 设计模式:策略模式