当前位置:   article > 正文

Pytorch中CrossEntropyLoss和BCE理解_pytorch bceloss

pytorch bceloss

一、CrossEntropyLoss

  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. def cross():
  5. x_input = torch.randn(4, 3) # 4个样本, 3个类别n=3,类别id分别是0,1,2
  6. print('x_input:\n', x_input)
  7. y_target = torch.tensor([1, 2, 0, 2]) # 4个样本的标签分别是1,2,0,2
  8. print('y_target:\n', y_target)
  9. softmax = nn.Softmax(dim=1) # softmax, 行处理, 每行的和为1
  10. soft = softmax(x_input) # soft = (exp(x1),...,exp(xn)) / sum(exp(xi))
  11. print('softmax:\n', soft) # shape(4,3)
  12. log_softmax = nn.LogSoftmax(dim=1) # log softmax, 行处理
  13. log_soft = log_softmax(x_input) # log_soft = (log(soft(1)),...,log(soft(n)))
  14. print('log_softmax:\n', log_soft) # shape(4,3)
  15. nll_loss = nn.NLLLoss() # negative log likelihood loss
  16. loss = nll_loss(log_soft, y_target) # loss = -sum(sum(log_soft(i)*1{i=label}))
  17. print('nll_loss:\n', loss) # shape(1)
  18. nll_loss2 = nn.NLLLoss(reduction='none') # negative log likelihood loss
  19. loss2 = nll_loss2(log_soft, y_target) # loss2 = -sum(log_soft(i)*1{i=label})
  20. print('nll_loss2:\n', loss2) # shape(4)
  21. cross_entropy_loss = nn.CrossEntropyLoss() # cross entropy loss = nll_loss
  22. ce_loss = cross_entropy_loss(x_input, y_target) # loss = 所有样本的平均交叉熵损失值
  23. print('ce_loss:\n', ce_loss) # shape(1)
  24. cross_entropy_loss2 = nn.CrossEntropyLoss(reduction='none') # cross entropy loss2 = nll_loss2
  25. ce_loss2 = cross_entropy_loss2(x_input, y_target) # loss = 每个样本的交叉熵损失值
  26. print('ce_loss:\n', ce_loss2) # shape(4)

x_input:
 tensor([[ 0.93692, -0.88950,  0.16872],
        [ 0.10782,  1.63110,  0.14352],
        [-0.07629, -0.26102,  1.89408],
        [ 0.62106,  0.37981, -0.04945]])
y_target:
 tensor([1, 2, 0, 2])
softmax:
 tensor([[0.61545, 0.09908, 0.28547],
        [0.15098, 0.69256, 0.15646],
        [0.11105, 0.09232, 0.79662],
        [0.43533, 0.34202, 0.22265]])
log_softmax:
 tensor([[-0.48541, -2.31182, -1.25360],
        [-1.89063, -0.36736, -1.85494],
        [-2.19774, -2.38247, -0.22737],
        [-0.83164, -1.07290, -1.50215]])
nll_loss:
 tensor(1.96666)
nll_loss2:
 tensor([2.31182, 1.85494, 2.19774, 1.50215])
ce_loss:
 tensor(1.96666)
ce_loss:
 tensor([2.31182, 1.85494, 2.19774, 1.50215])

二、BCEWithLogitsLoss

  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. def binary_cross():
  5. x_input = torch.rand(4, 3) # 4个样本, 3个类别n=3,类别id分别是0,1,2 range=[0,1]
  6. print('x_input:\n', x_input)
  7. y_target = torch.empty(4, 3).random_(2) # shape as x_input shape range={0., 1.}
  8. print('y_target:\n', y_target)
  9. bce_loss = nn.BCELoss()
  10. loss = bce_loss(x_input, y_target) # loss = 所有样本的平均二值交叉熵损失值
  11. print('bce_loss:\n', loss) # shape[1]
  12. bcel_loss1 = nn.BCEWithLogitsLoss()
  13. loss1 = bcel_loss1(x_input, y_target) # loss = 所有样本的平均二值交叉熵损失值
  14. print('bcel_loss:\n', loss1) # shape[1]
  15. bcel_loss2 = nn.BCEWithLogitsLoss(reduction='none')
  16. loss2 = bcel_loss2(x_input, y_target) # loss2 = 二值交叉熵损失值
  17. print('bcel_loss2:\n', loss2) # shape[4,3]
  18. loss3 = F.binary_cross_entropy(x_input, y_target, reduction='none') # as nn.BCELoss
  19. print('loss3:\n', loss3) # shape[4,3]
  20. loss4 = F.binary_cross_entropy_with_logits(x_input, y_target, reduction='none') # as nn.BCEWithLogitsLoss
  21. print('loss4:\n', loss4) # shape[4,3]

x_input:
 tensor([[0.60999, 0.37137, 0.19881],
        [0.05420, 0.96486, 0.95869],
        [0.63311, 0.76424, 0.54986],
        [0.55075, 0.63117, 0.80683]])
y_target:
 tensor([[1., 0., 0.],
        [0., 0., 1.],
        [1., 1., 1.],
        [0., 0., 1.]])
bce_loss:
 tensor(0.66356)
bcel_loss:
 tensor(0.67972)
bcel_loss2:
 tensor([[0.43396, 0.89597, 0.79748],
        [0.72062, 1.28769, 0.32454],
        [0.42588, 0.38232, 0.45554],
        [1.00597, 1.05772, 0.36899]])
loss3:
 tensor([[0.49431, 0.46421, 0.22165],
        [0.05573, 3.34830, 0.04219],
        [0.45712, 0.26887, 0.59809],
        [0.80018, 0.99742, 0.21464]])
loss4:
 tensor([[0.43396, 0.89597, 0.79748],
        [0.72062, 1.28769, 0.32454],
        [0.42588, 0.38232, 0.45554],
        [1.00597, 1.05772, 0.36899]])

Process finished with exit code 0
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/763322
推荐阅读
相关标签
  

闽ICP备14008679号