简体   繁体   English

pytorch BCELOSS vs binary_cross_entropy:权重的使用不同

[英]pytorch BCELOSS vs binary_cross_entropy: usage of weights different

I observed that the weights in BCELoss do weight each sample in the batch, but the weights in binary_cross_entropy weight the loss depending on if 0 or 1 was the true label.我观察到 BCELoss 中的权重确实对批次中的每个样本进行加权,但是 binary_cross_entropy 中的权重根据 0 或 1 是真实标签来加权损失。 That is very weird, since the docs of binary_cross_entropy docs of binary_cross_entropy have a redirection link to the docs of BCELoss .这很奇怪,因为 binary_cross_entropy 的 binary_cross_entropy 文档有一个指向BCELoss 文档的重定向链接。

This is a short example that only in binary_cross_entropy the loss is affected if 1 or 0 was the true label with corresponding the same predictions:这是一个简短的例子,只有在 binary_cross_entropy 中,如果 1 或 0 是对应相同预测的真实标签,损失才会受到影响:

import torch
import torch.nn.functional as F
from torch.nn import BCELoss

predictions1 = torch.tensor([[0.2,0.8]],dtype=torch.float)
predictions2 = torch.tensor([[0.8,0.2]],dtype=torch.float)
true_val1 = torch.tensor([[0,1]],dtype=torch.float)
true_val2 = torch.tensor([[1,0]],dtype=torch.float)
weights = torch.tensor([[0.6,5]],dtype=torch.float)

bce_loss = BCELoss(weight=weights)
bce_loss(predictions1, true_val1)
>>> out: tensor(0.6248)
bce_loss(predictions2, true_val2)
>>> out: tensor(0.6248)
F.binary_cross_entropy(input=predictions1, target=true_val1, weight=weights)
>>> out: tensor(0.6248)
F.binary_cross_entropy_with_logits(input=predictions2, target=true_val2, weight=weights)
>>> out: tensor(2.1067)

Do I just not see how this makes sense?我只是不明白这是怎么回事吗? Why are there even two methods for the binary cross entropy loss in pytorch?为什么pytorch中的二元交叉熵损失甚至有两种方法?

nothing.没有什么。 I wasn't looking into the source code of pytorch.我没有查看 pytorch 的源代码。

BCELoss class forward method is based on the F.binary_cross_entropy link . BCELoss 类的 forward 方法基于 F.binary_cross_entropy 链接 So it returns the same result because it is the same.所以它返回相同的结果,因为它是相同的。 In the most cases we initiate the class and pass the model outputs.在大多数情况下,我们启动类并传递模型输出。

class BCELoss(_WeightedLoss):
      def __init__(self, weight: Optional[Tensor] = None, size_average=None, reduce=None, reduction: str = 'mean') -> None:
          super(BCELoss, self).__init__(weight, size_average, reduce, reduction)


      def forward(self, input: Tensor, target: Tensor) -> Tensor:
          return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)

However there might be use cases there we want to use the function instead.但是,可能存在我们想要使用该函数的用例。

nn.BCEWithLogitsLoss includes sigmoid activation, so you don't need to add it explicitly, as shown with just BCE: nn.BCEWithLogitsLoss 包括 sigmoid 激活,因此您无需显式添加它,如仅使用 BCE 所示:

loss = F.binary_cross_entropy(torch.sigmoid(input), target) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM