Pytorch:在 BCEWithLogitsLoss 中加权,但使用 'weight' 而不是 'pos_weight'

Pytorch: Weighting in BCEWithLogitsLoss, but with 'weight' instead of 'pos_weight'

我正在寻找如何使用 BCEWithLogitsLoss 进行 class 加权。

https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html

关于如何使用 pos_weight 的示例对我来说似乎很清楚。如果负样本比正样本多 3 倍,则可以设置 pos_weight=3

weight参数是否做同样的事情?

说我设置的weight=torch.tensor([1, 3])。和pos_weight=3

一样吗

此外,weight 是否标准化? weight=torch.tensor([1, 3])weight=torch.tensor([3, 9]) 相同,还是它们对损失大小的影响不同?

它们是不同的东西。 pos_weight 是尺码 n_classes。 weight 是尺寸 batch_size。在您链接的页面的公式中,weight 指的是 w_n 变量,而 pos_weight 指的是 p_c 变量。