首先要注意的是PyTorch是Channel First模式,对于图片而言格式应该是(B,C,H,W),这个尤其重要,搞错了一定会报错。
大写的CrossEntropyLoss表示是一个类,需要先实例化
loss = torch.nn.CrossEntropyLoss()
pred = torch.zeros((4,2,32,32),dtype=torch.float32)
target =torch.ones((4,32,32),dtype=torch.long)
loss_nn = loss(pred,target)
print(loss_nn)
输出: tensor(0.6931)
需要注意的是:
pred = torch.zeros((4,2,32,32),dtype=torch.float32)
target =torch.ones((4,32,32),dtype=torch.long)
loss_F = F.cross_entropy(pred, target)
print(loss_F)
输出:tensor(0.6931)
print(loss_F == loss_nn)
输出:tensor(True)
torch.manual_seed(1)
pred = torch.randn((4,32,32),dtype=torch.float32)
target = torch.ones((4,32,32),dtype=torch.float32)
sig = torch.nn.Sigmoid()
pred = sig(pred)
loss_nn = torch.nn.BCELoss(reduction='mean')(pred,target)
print(loss_nn)
输出:tensor(0.8030)
注意点如下:
torch.manual_seed(1)
pred = torch.randn((4,32,32),dtype=torch.float32)
target = torch.ones((4,32,32),dtype=torch.float32)
sig = torch.nn.Sigmoid()
pred = sig(pred)
loss_f = torch.nn.functional.binary_cross_entropy(pred,target)
print(loss_f)
输出:tensor(0.8030)
print(loss_nn==loss_f)
输出:tensor(True)
这个类和BCELoss的差别在于多了sigmoid操作
torch.manual_seed(1)
pred = torch.randn((4,32,32),dtype=torch.float32)
target = torch.ones((4,32,32),dtype=torch.float32)
loss_nn = torch.nn.BCEWithLogitsLoss()(pred,target)
print(loss_nn)
输出:tensor(0.8030)
torch.manual_seed(1)
pred = torch.randn((4,32,32),dtype=torch.float32)
target = torch.ones((4,32,32),dtype=torch.float32)
loss_f = torch.nn.functional.binary_cross_entropy_with_logits(pred,target)
print(loss_f)
输出:tensor(0.8030)
print(loss_f==loss_nn)
输出:tensor(True)