Open rentainhe opened 2 years ago
# nll loss会取出target对应index的值,然后求和后作sum或者mean操作后求负数
predict = torch.Tensor([[2, 3, 1],
[3, 7, 9]])
label = torch.tensor([1, 2]) # 第一个sample对应index=1的位置为3, 第二个sample对应index为2的值为9
loss = F.nll_loss(predict, label)
print(loss) # tensor(-6.)
>>> torch.softmax(predict, dim=-1)
tensor([[0.2447, 0.6652, 0.0900],
[0.0022, 0.1189, 0.8789]])
>>> torch.log(torch.softmax(predict, dim=-1))
tensor([[-1.4076, -0.4076, -2.4076],
[-6.1291, -2.1291, -0.1291]])
>>> predict = torch.log(torch.softmax(predict, dim=-1))
>>> F.nll_loss(predict, label)
tensor(0.2684)
softmax
操作后,所有的预测prob都被归一化到0-1区间,取-log
后,预测的值越接近正确target,loss越小,并且如果越远离target,loss越大,惩罚力度也越大https://blog.csdn.net/qq_27182145/article/details/108509227
Y_pred
为预测的结果Y_true
为真实标签N
ϵ
参考文章
CrossEntropy = Log + Softmax + NLL_Loss