二分类任务交叉熵损失函数定义
多分类任务的交叉熵损失函数定义为:
其中
是向量,
表示样本预测为第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)