BCEWithLogitsLoss參數(shù)pos_weight樣本不均衡問(wèn)題

下面是具體的參數(shù):

1. pos_weight:
  • 處理樣本不均衡問(wèn)題
    torch.nn.BCEWithLogitsLoss(weight=None, size_average=None, reduce=None, reduction='mean', pos_weight=None)


  • 其中* pos_weight (Tensor*, *optional) – a weight of positive examples. Must be a vector with length equal to the number of classes.
  • pos_weight里是一個(gè)tensor列表,需要和標(biāo)簽個(gè)數(shù)相同,比如現(xiàn)在有一個(gè)多標(biāo)簽分類,類別有200個(gè),那么 pos_weight 就是為每個(gè)類別賦予的權(quán)重值,長(zhǎng)度為200,官方給出的例子是:
target = torch.ones([10, 64], dtype=torch.float32)  # 64 classes, batch size = 10
output = torch.full([10, 64], 1.5)  # A prediction (logit)
pos_weight = torch.ones([64])  # All weights are equal to 1
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
criterion(output, target)  # -log(sigmoid(1.5))
  • 如果現(xiàn)在是二分類,只需要將正樣本loss的權(quán)重寫(xiě)上即可,比如我們有正負(fù)兩類樣本,正樣本數(shù)量為100個(gè),負(fù)樣本為400個(gè),我們想要對(duì)正負(fù)樣本的loss進(jìn)行加權(quán)處理,將正樣本的loss權(quán)重放大4倍,通過(guò)這樣的方式緩解樣本不均衡問(wèn)題:
criterion = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([4]))

-- pos_weight (Tensor, optional): a weight of positive examples.
--Must be a vector with length equal to the number of classes.

參考:
BCEWithLogitsLoss樣本不均衡的處理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容