【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()

nn.BCELoss

  • 1、nn.BCELoss
  • 2、使用场景
  • 3、nn.BCELoss 计算公式
  • 4、torch.nn.BCEWithLogitsLoss() 与 nn.BCELoss() 的区别
  • 5、torch.nn.BCELoss() 函数
  • 6、torch.nn.BCEWithLogitsLoss() 函数

1、nn.BCELoss

  • nn.BCELoss() 是 二元交叉熵损失函数 (Binary Cross Entropy Loss)
  • 适用于二分类问题,即
    • 模型的输出为一个概率值,表示样本属于某一类的概率
    • 标签为二元值:0 或 1
  • nn.BCELoss() 计算的是二元交叉熵损失,也称为对数损失,它将模型 预测值 和 真实标签值 之间的差异转化为一个标量损失值,用于衡量模型预测的准确性。

2、使用场景

假设有一个 二分类任务:判断一张图片中是否包含猫。

我们可以定义一个二元分类模型,用 Sigmoid 输出一个概率值,表示样本属于猫的概率。 标签值为 0 和 1。

使用以下代码构建模型和损失函数:

import torch
import torch.nn as nn

class CatClassifier(nn.Module):
    def __init__(self):
        super(CatClassifier, self).__init__()
        self.fc = nn.Linear(3*256*256, 1)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        x = self.sigmoid(x)
        return x

model = CatClassifier()
criterion = nn.BCELoss()

在每次迭代时,我们可以通过以下代码计算损失:

outputs = model(inputs)
loss = criterion(outputs, labels.float())

3、nn.BCELoss 计算公式

【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()

其中:

  • 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 表示样本数量
  • 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 表示第 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 个样本的实际标签
  • 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 表示第 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 个样本的预测值

如果 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss(),则第一项 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 生效,第二项 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 失效;
如果 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss(),则第一项失效,第二项生效。

因为 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 值的范围是 0 ~ 1, 所以,【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 的值都是负数,如下图红色曲线。 所以,损失函数的最前面有个负号,将损失值变为正数。

4、torch.nn.BCEWithLogitsLoss() 与 nn.BCELoss() 的区别

nn.BCELoss() 的输入是 二元分类模型的预测值 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 和 实际标签 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()。并且【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 的范围是 [0,1],因为二元分类模型内部已经对预测结果做了 sigmoid 处理。
【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()

torch.nn.BCEWithLogitsLoss() 的输入是也是 二元分类模型的输出值 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 和实际标签 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()不同的是 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 在模型内部没有经过 sigmoid 处理,是任意实数。这种情况下,sigmoid 处理就被放到了损失函数中,所以,torch.nn.BCEWithLogitsLoss() 函数内部的计算过程是先对 【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss() 应用 sigmoid 函数,将其映射到 [0,1] 范围内,然后使用二元交叉熵计算预测值和实际标签之间的损失值。
【pytorch】二元交叉熵损失函数 nn.BCELoss() 与 torch.nn.BCEWithLogitsLoss()

另外,torch.nn.BCEWithLogitsLoss() 还支持设置 pos_weight 参数,用于处理样本不平衡的问题。而 nn.BCELoss() 不支持设置 pos_weight 参数。

5、torch.nn.BCELoss() 函数

torch.nn.BCELoss(weight=None, size_average=None, reduce=None, reduction='mean')

参数说明:

  • weight :用于样本加权的权重张量。如果给定,则必须是一维张量,大小等于输入张量的大小。默认值为 None。
  • reduction :指定如何计算损失值。可选值为 ‘none’、‘mean’ 或 ‘sum’。默认值为 ‘mean’。

6、torch.nn.BCEWithLogitsLoss() 函数

torch.nn.BCEWithLogitsLoss(weight=None, 
                           size_average=None, 
                           reduce=None, 
                           reduction='mean', 
                           pos_weight=None)

参数说明:

  • weight:用于对每个样本的损失值进行加权。默认值为 None。
  • reduction:指定如何对每个 batch 的损失值进行降维。可选值为 ‘none’、‘mean’ 和 ‘sum’。默认值为 ‘mean’。
  • pos_weight:用于对正样本的损失值进行加权。可以用于处理样本不平衡的问题。例如,如果正样本比负样本少很多,可以设置 pos_weight 为一个较大的值,以提高正样本的权重。默认值为 None。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年11月23日
下一篇 2023年11月23日

相关推荐