pytorch BCEWithLogitsLoss 计算 pos_weight

pytorch BCEWithLogitsLoss calculating pos_weight

我有一个神经网络,如下所示,用于二进制预测。我的 classes 严重不平衡,class 1 仅出现 2% 的次数。仅显示最后几层

self.batch_norm2 = nn.BatchNorm1d(num_filters)

self.fc2 = nn.Linear(np.sum(num_filters), fc2_neurons)

self.batch_norm3 = nn.BatchNorm1d(fc2_neurons)

self.fc3 = nn.Linear(fc2_neurons, 1)

我的损失如下。这是计算 pos_weight 参数的正确方法吗?我查看了这个 link 的官方文档,它表明 pos_weight 需要为每个 class 设置一个值以实现 multiclass classification。不确定二进制 class 是否是不同的情况。我尝试输入 2 个值,但出现错误

我的问题:对于二进制问题,pos_weight 是一个单一的值,不像 multiclass classification 它需要一个list/array 长度等于 classes?

BCE_With_LogitsLoss=nn.BCEWithLogitsLoss(pos_weight=class_wts[0]/class_wts[1])

我的 y 变量是一个单一的变量,它有 0 或 1 来代表实际 class 并且神经网络输出一个单一的值

---------------------------------------- ----------更新1

根据 Shai 的回答,我有以下问题:

  1. BCEWithLogitsLoss - 如果是多class问题那么如何使用pos_weigh参数?
  2. 有没有在pytorch中使用focal loss的例子?我找到了一些链接,但其中大部分都是旧的 - 约会 2 或 3 年或更长时间
  3. 为了训练,我对 class 1. 焦点损失仍然适用吗?

pos_weight的文档确实有点不清楚。对于 BCEWithLogitsLoss pos_weight 应该是大小为 1 的 torch.tensor:

BCE_With_LogitsLoss=nn.BCEWithLogitsLoss(pos_weight=torch.tensor([class_wts[0]/class_wts[1]]))

但是,在您的情况下,pos class 仅出现 2% 的次数,我认为设置 pos_weight 是不够的。
请考虑使用 :
Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár Focal Loss for Dense Object Detection (ICCV 2017).
除了描述 Focal loss 之外,这篇论文还很好地解释了为什么 CE loss 在不平衡的情况下表现如此糟糕。我强烈推荐阅读这篇论文。

列出了其他备选方案