torch.nn.BCEWithLogitsLoss用法介绍

这篇具有很好参考价值的文章主要介绍了torch.nn.BCEWithLogitsLoss用法介绍。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

self.bce = nn.BCEWithLogitsLoss(reduction='none'), None的使用方法可以见官网pytorch代码文档
nn.bcewithlogitsloss 用法,torch基本函数用法介绍,pytorch
代码举例

import torch
a = torch.rand((1, 3, 3))
target = torch.tensor([[[1, 0, 0],
                        [0, 1, 0],
                        [0, 0, 0]]])
print(a)
'''
ouput:tensor([[[0.2070, 0.8432, 0.2494],
         [0.5782, 0.4587, 0.1135],
         [0.9794, 0.8516, 0.4418]]])
'''
b = torch.nn.BCEWithLogitsLoss(reduction='none')(a, target.to(a.dtype))
print(b)
'''
output: tensor([[[0.5950, 1.2011, 0.8256],
         [1.0234, 0.4899, 0.7515],
         [1.2982, 1.2070, 0.9383]]])
'''
c = torch.nn.BCEWithLogitsLoss(reduction='mean')(a, target.to(a.dtype))
print(c)
'''
output:tensor(0.9256)
'''
d = torch.nn.BCEWithLogitsLoss(reduction='sum')(a, target.to(a.dtype))
print(d)
'''
output:tensor(8.3301)
'''

举例的代码中target中的计算方法是这样的(resuction='none')

对于target[0][0][0]=1=yn, a[0][0][0]=0.2070=xn, 因此,对于ln

0.5950 = − ( 1 × ln ⁡ σ ( 0.2070 ) + 0 × ln ⁡ ( 1 − σ ( 0.2070 ) ) ) 0.5950 = -\left ( 1 \times \ln{\sigma\left ( 0.2070 \right ) } + 0 \times\ln{ \left ( 1 - \sigma \left ( 0.2070\right ) \right ) } \right ) 0.5950=(1×lnσ(0.2070)+0×ln(1σ(0.2070)))
对于target[0][0][1]=0=yn, a[0][0][1]=0.8432=xn, 因此,对于ln
1.2011 = − ( 0 × ln ⁡ σ ( 0.8432 ) + 1 × ln ⁡ ( 1 − σ ( 0.8432 ) ) ) 1.2011= -\left ( 0 \times \ln{\sigma\left ( 0.8432\right ) } + 1 \times\ln{ \left ( 1 - \sigma \left ( 0.8432\right ) \right ) } \right ) 1.2011=(0×lnσ(0.8432)+1×ln(1σ(0.8432)))

其中 σ \sigma σ是Sigmoid函数, 以此类推, 可以最终得到output: tensor([[[0.5950, 1.2011, 0.8256],[1.0234, 0.4899, 0.7515],[1.2982, 1.2070, 0.9383]]]), 如果reduction=‘mean’的时候, 就是reduction=‘none’的output求平均值, 最终可以得到output:tensor(0.9256),如果reduction='sum',那就是reduction=‘none’的output求和,最终可以得到output:tensor(8.3301)

可以验证一下上面的代码,可以看到结果是一样的

a = torch.tensor([[[0.2070, 0.8432, 0.2494],
                   [0.5782, 0.4587, 0.1135],
                   [0.9794, 0.8516, 0.4418]]])
target = torch.tensor([[[1, 0, 0],
                        [0, 1, 0],
                        [0, 0, 0]]])
sa = torch.nn.Sigmoid()(a)
result = -(target * torch.log(sa) + (1 - target) * torch.log(1 - sa))
'''
output: tensor([[[0.5950, 1.2011, 0.8256],
         [1.0235, 0.4899, 0.7515],
         [1.2982, 1.2070, 0.9382]]])
'''
result_mean = result.mean()
'''
output: tensor(0.9256)
'''
result_sum = result.sum()
'''
output: tensor(8.3300)
'''

如果是bs = 2,reduction='mean'reduction='sum'的效果也是一样的,最终的结果也是一个值。代码如下, 可以看到均值不变, 求和的时候扩大2倍文章来源地址https://www.toymoban.com/news/detail-800715.html

import torch
# a = torch.rand((1, 3, 3))
a = torch.tensor([[[0.2070, 0.8432, 0.2494],
         [0.5782, 0.4587, 0.1135],
         [0.9794, 0.8516, 0.4418]], 
         [[0.2070, 0.8432, 0.2494],
         [0.5782, 0.4587, 0.1135],
         [0.9794, 0.8516, 0.4418]]])
target = torch.tensor([[[1, 0, 0],
                        [0, 1, 0],
                        [0, 0, 0]],
                        [[1, 0, 0],
                        [0, 1, 0],
                        [0, 0, 0]]])

b = torch.nn.BCEWithLogitsLoss(reduction='none')(a, target.to(a.dtype))
print(b)
'''
output: tensor([[[0.5950, 1.2011, 0.8256],
                 [1.0235, 0.4899, 0.7515],
                 [1.2982, 1.2070, 0.9382]],

		        [[0.5950, 1.2011, 0.8256],
		         [1.0235, 0.4899, 0.7515],
		         [1.2982, 1.2070, 0.9382]]])
'''

c = torch.nn.BCEWithLogitsLoss(reduction='mean')(a, target.to(a.dtype))
print(c)
'''
output: tensor(0.9256)
'''

d = torch.nn.BCEWithLogitsLoss(reduction='sum')(a, target.to(a.dtype))
print(d)
'''
output: tensor(16.6601)
'''

sa = torch.nn.Sigmoid()(a)
result = -(target * torch.log(sa) + (1 - target) * torch.log(1 - sa))
print(result)
'''
output: tensor([[[0.5950, 1.2011, 0.8256],
		         [1.0235, 0.4899, 0.7515],
		         [1.2982, 1.2070, 0.9382]],

		        [[0.5950, 1.2011, 0.8256],
		         [1.0235, 0.4899, 0.7515],
		         [1.2982, 1.2070, 0.9382]]])
'''

result_mean = result.mean()
'''
output: tensor(0.9256)
'''
result_sum = result.sum()
'''
output: tensor(16.6601)
'''

到了这里,关于torch.nn.BCEWithLogitsLoss用法介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 八百字讲清楚——BCEWithLogitsLoss二分类损失函数

    BCEWithLogitsLoss 是一种用于二分类问题的损失函数,它将 Sigmoid 函数和二元交叉熵损失结合在一起。 假设我们有一个大小为 N N N 的二分类问题,其中每个样本 x i x_i x i ​ 有一个二元标签 y i ∈ 0 , 1 y_iin {0,1} y i ​ ∈ 0 , 1 ,并且我们希望预测每个样本的概率为 p i ∈ [ 0 , 1 ]

    2023年04月19日
    浏览(27)
  • Pytorch:torch.nn.Module.apply用法详解

    torch.nn.Module.apply 是 PyTorch 中用于递归地应用函数到模型的所有子模块的方法。它允许对模型中的每个子模块进行操作,比如初始化权重、改变参数类型等。 以下是关于 torch.nn.Module.apply 的示例: 1. 语法 Module:PyTorch 中的神经网络模块,例如 torch.nn.Module 的子类。 fn:要应用到

    2024年01月15日
    浏览(37)
  • 【PyTorch】进阶学习:探索BCEWithLogitsLoss的正确使用---二元分类问题中的logits与标签形状问题

    【PyTorch】进阶学习:探索BCEWithLogitsLoss的正确使用—二元分类问题中的logits与标签形状问题 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平

    2024年03月11日
    浏览(35)
  • 小知识点系列(十一) 本文(2万字) | BCELoss | BCEWithLogitsLoss | CrossEntropyLoss | Smooth L1 Loss | NLLLOSS |

    点击进入专栏: 《人工智能专栏》 Python与Python | 机器学习 | 深度学习 | 目标检测 | YOLOv5及其改进 | YOLOv8及其改进 | 关键知识点 | 各种工具教程 官网 torch.nn - PyTorch中文文档 (pytorch-cn.readthedocs.io)

    2024年01月21日
    浏览(32)
  • python中的torch.nn.Softmax() 用法和例子 dim=1 dim=2

    torch.nn.Softmax() 是 PyTorch 中的一个类,用于实现 softmax 函数。softmax 函数是一种常用的激活函数,它可以将一个向量转换成一个概率分布,使得每个元素都是非负数且和为 1。softmax 函数通常在分类问题中使用,可以将一个多分类问题转换成多个二分类问题,从而得到每个类别的

    2024年02月16日
    浏览(88)
  • 深度学习之pytorch 中 torch.nn介绍

    pytorch 中必用的包就是 torch.nn,torch.nn 中按照功能分,主要如下有几类: 1. Layers(层):包括全连接层、卷积层、池化层等。 2. Activation Functions(激活函数):包括ReLU、Sigmoid、Tanh等。 3. Loss Functions(损失函数):包括交叉熵损失、均方误差等。 4. Optimizers(优化器):包括

    2024年02月22日
    浏览(35)
  • 【torch.nn.Sequential】序列容器的介绍和使用

    nn.Sequential是一个有序的容器,该类将按照传入构造器的顺序,依次创建相应的函数,并记录在Sequential类对象的数据结构中,同时以神经网络模块为元素的有序字典也可以作为传入参数。 因此,Sequential可以看成是有多个函数运算对象,串联成的神经网络,其返回的是Module类型

    2024年02月04日
    浏览(34)
  • Pytorch学习笔记(5):torch.nn---网络层介绍(卷积层、池化层、线性层、激活函数层)

     一、卷积层—Convolution Layers  1.1 1d / 2d / 3d卷积 1.2 卷积—nn.Conv2d() nn.Conv2d 1.3 转置卷积—nn.ConvTranspose nn.ConvTranspose2d  二、池化层—Pooling Layer (1)nn.MaxPool2d (2)nn.AvgPool2d (3)nn.MaxUnpool2d  三、线性层—Linear Layer  nn.Linear  四、激活函数层—Activate Layer (1)nn.Sigmoid  (

    2024年01月20日
    浏览(34)
  • 【torch.nn.Fold】和【torch.nn.Unfold】

    torhc.nn.Unfold的功能: 从一个batch的样本中,提取出滑动的局部区域块 patch (也就是卷积操作中的提取kernel filter对应的滑动窗口)把它按照顺序展开,得到的特征数就是 通道数*卷积核的宽*卷积核的高 , 下图中的 L 就是滑动完成后总的 patch的个数 。 举个例子: 下图中的红框

    2024年02月13日
    浏览(24)
  • 【torch.nn.PixelShuffle】和 【torch.nn.UnpixelShuffle】

    PixelShuffle是一种上采样方法,它将形状为 ( ∗ , C × r 2 , H , W ) (∗, Ctimes r^2, H, W) ( ∗ , C × r 2 , H , W ) 的张量重新排列转换为形状为 ( ∗ , C , H × r , W × r ) (∗, C, Htimes r, Wtimes r) ( ∗ , C , H × r , W × r ) 的张量: 举个例子 输入的张量大小是 (1,8,2,3) ,PixelShuffle的 缩放因子是

    2024年02月13日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包