多分类:CrossEntroploss()损失函数的简单实操:Mini-Batch:batch_size=3

import torch
criterion=torch.nn.CrossEntropyLoss()
Y=torch.LongTensor([2,0,1]) #交叉熵损失函数要求标签y的值为LongTensor
Y_pred1=torch.Tensor([[0.1,0.2,0.9],
                      [1.1,0.1,0.2],
                      [0.2,2.1,0.1]])
Y_pred2=torch.Tensor([[0.1,0.2,0.9],
                      [0.2,0.3,0.5],
                      [0.2,0.2,0.5]])
l1=criterion(Y_pred1,Y)
l2=criterion(Y_pred2,Y)
print('Batch Loss1 =',l1.data,'\nBatch Loss1 =',l2.data)

猜你喜欢

转载自blog.csdn.net/qq_21686871/article/details/114257066