pytorch BCEWithLogitsLoss pos_weight参数解疑
生活随笔
收集整理的這篇文章主要介紹了
pytorch BCEWithLogitsLoss pos_weight参数解疑
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
下面的代碼來自Distraction-aware Shadow Detection
class MyWcploss(nn.Module):def __init__(self):super(MyWcploss, self).__init__()def forward(self, pred, gt):eposion = 1e-10sigmoid_pred = torch.sigmoid(pred)count_pos = torch.sum(gt)*1.0+eposioncount_neg = torch.sum(1.-gt)*1.0beta = count_neg/count_posbeta_back = count_pos / (count_pos + count_neg)bce1 = nn.BCEWithLogitsLoss(pos_weight=beta)loss = beta_back*bce1(pred, gt)return loss上述代碼實現(xiàn)了:
第一次看的時候沒有看懂,所以去查BCEWithLogitsLoss,
對于pos_weight參數(shù)很疑惑,看完下面的公式,就清楚了。
詳細可見:BCEWithLogitsLoss pytorch 官方文檔
總結
以上是生活随笔為你收集整理的pytorch BCEWithLogitsLoss pos_weight参数解疑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: easyboot-code-genera
- 下一篇: PageHelper自定义count