二分类交叉熵损失函数
import torch
import torch.nn.functional as F
pred = torch.tensor([[-0.4089,-1.2471,0.5907],[-0.4897,-0.8267,-0.7349],[0.5241,-0.1246,-0.4757]]).cuda()
print(pred)
'''
tensor([[-0.4089, -1.2471, 0.5907],
[-0.4897, -0.8267, -0.7349],
[ 0.5241, -0.1246, -0.4757]], device='cuda:0')
'''
v = torch.sigmoid(pred)
print(v)
'''
tensor([[0.3992, 0.2232, 0.6435],
[0.3800, 0.3043, 0.3241],
[0.6281, 0.4689, 0.3833]], device='cuda:0')
'''
target = torch.FloatTensor([[0,1,1],[0,0,1],[1,0,1]]).cuda()
print(F.binary_cross_entropy_with_logits(pred,target))
'''
tensor(0.7194, device='cuda:0')
'''
print(F.binary_cross_entropy(v,target))
'''
tensor(0.7194, device='cuda:0')
'''
nn.CrossEntropyLoss
https://mathpretty.com/12068.html