pytorch如何定义损失函数_PyTorch学习笔记——二分类交叉熵损失函数

6b13a2ed42f638c2b4696a9ab3eb834d.png

二分类任务交叉熵损失函数定义

多分类任务的交叉熵损失函数定义为:

其中

是向量,
表示样本预测为第c类的概率。

如果是二分类任务的话,因为只有正例和负例,且两者的概率和是1,所以不需要预测一个向量,只需要预测一个概率就好了,损失函数定义简化如下:

其中

是模型预测样本是正例的概率,
是样本标签,如果样本属于正例,取值为1,否则取值为0。

PyTorch中二分类交叉熵损失函数的实现

PyTorch提供了两个类来计算二分类交叉熵(Binary Cross Entropy),分别是BCELoss() 和BCEWithLogitsLoss()

  • torch.nn.BCELoss()

类定义如下

torch

用N表示样本数量,

表示预测第n个样本为正例的
概率
表示第n个样本的标签,则:

举个例子

import torch
import torch.nn as nn

model = nn.Sequential(
    nn.Linear(10, 1),
    nn.Sigmoid()
)
criterion = nn.BCELoss()

x = torch.randn(16, 10)  # (16, 10)
y = torch.empty(16).random_(2)  # shape=(16, ) 其中每个元素值为0或1

out = model(x)  # (16, 1)
out = out.squeeze(dim=-1)  # (16, )

loss = criterion(out, y)
  • torch.nn.BCEWithLogitsLoss()

类定义如下

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

用N表示样本数量,

表示预测第n个样本为正例的
得分
表示第n个样本的标签,
表示sigmoid函数,则:

这个类将Sigmoid()和BCELoss()整合起来,比 纯粹使用BCELoss()+Sigmoid()更数值稳定。

This loss combines a Sigmoid layer and the BCELoss in one single class. This version is more numerically stable than using a plain Sigmoid followed by a BCELoss as, by combining the operations into one layer, we take advantage of the log-sum-exp trick for numerical stability.

举个例子

import torch
import torch.nn as nn

model = nn.Linear(10, 1)
criterion = nn.BCEWithLogitsLoss()

x = torch.randn(16, 10)
y = torch.empty(16).random_(2)  # (16, )

out = model(x)  # (16, 1)
out = out.squeeze(dim=-1)  # (16, )

loss = criterion(out, y)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值