pytorch如何定义损失函数_PyTorch学习笔记——二分类交叉熵损失函数
二分类任务交叉熵损失函数定义多分类任务的交叉熵损失函数定义为: 其中是向量,表示样本预测为第c类的概率。如果是二分类任务的话,因为只有正例和负例,且两者的概率和是1,所以不需要预测一个向量,只需要预测一个概率就好了,损失函数定义简化如下: 其中是模型预测样本是正例的概率,是样本标签,如果样本属于正例,取值为1,否则取值为0。PyTorch中二分类交叉熵损失函数的实现PyTorch提...
·

二分类任务交叉熵损失函数定义
多分类任务的交叉熵损失函数定义为:
其中
如果是二分类任务的话,因为只有正例和负例,且两者的概率和是1,所以不需要预测一个向量,只需要预测一个概率就好了,损失函数定义简化如下:
其中
PyTorch中二分类交叉熵损失函数的实现
PyTorch提供了两个类来计算二分类交叉熵(Binary Cross Entropy),分别是BCELoss() 和BCEWithLogitsLoss()
- torch.nn.BCELoss()
类定义如下
torch
用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表示样本数量,
这个类将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)