import torch
predict = torch.randn((4,3))
# crossentropyloss不需要predict的概率为1,predict为logits
# predict = torch.nn.functional.softmax(predict,dim = 1)
target = torch.empty(4,dtype=torch.long).random_(3)
loss = torch.nn.CrossEntropyLoss()
loss_ = loss(predict,target)
print(predict)
print(target)
print(loss_)
#输出:
tensor([[0.2073, 0.0603, 0.7324],
[0.0946, 0.3523, 0.5530],
[0.6322, 0.0538, 0.3140],
[0.3543, 0.1425, 0.5033]])
tensor([0, 2, 1, 2])
tensor(1.1275)
公众号
过去已逝,未来太远,只争今朝