二分类损失

  • NCELoss
  • NCEWithLogitsLoss
  1. import torch.nn as nn
  2. import torch
  3. import numpy as np
  4. def bce_loss(pred, y):
  5. return -y*np.log(pred) - (1-y)*np.log(1-pred)
  6. m = nn.Sigmoid()
  7. input = torch.randn(3, requires_grad=True)
  8. input_m = m(input)
  9. target = torch.empty(3).random_(2)
  10. print("predict origin:", input.data)
  11. print("predict after sigmoid:", input_m.data)
  12. print("actual label:", target)
  13. print("BCE Loss:", nn.BCELoss(reduction='none')(input_m, target).data)
  14. print("BCEwithLogitsLoss:", nn.BCEWithLogitsLoss(reduction='none')(input, target).data)
  15. print("BCE Loss Mean:", nn.BCELoss(reduction='mean')(input_m, target).data)
  16. input_m = input_m.detach().numpy()
  17. target = target.detach().numpy()
  18. print("Loss DIY", bce_loss(input_m, target))
  19. -- output
  20. predict origin: tensor([0.9719, 0.0456, 0.0118])
  21. predict after sigmoid: tensor([0.7255, 0.5114, 0.5029])
  22. actual label: tensor([1., 0., 0.])
  23. BCE Loss: tensor([0.3209, 0.7162, 0.6990])
  24. BCEwithLogitsLoss: tensor([0.3209, 0.7162, 0.6990])
  25. BCE Loss Mean: tensor(0.5787)
  26. Loss DIY [0.32090703 0.71618414 0.6990468 ]

根据官方文档,计算结果与手动计算结果一致,pytorch默认会是求平均。