美文网首页
pytorch CrossEntropyLoss(), Soft

pytorch CrossEntropyLoss(), Soft

作者: 不太聪明的亚子 | 来源:发表于2020-05-14 09:32 被阅读0次

softmax(input, target)前面分析过其中的dim,就不多说,给出的结果是一个样本在C个类别上的概率分别是多少,概率之和为1.

logSoftmax(input, target)就是先softmax,然后将结果log一下,softmax的数值∈[0,1],log以后就是负无穷到0之间,这样做的好处解决softmax可能带来的上溢出和下溢出问题,加快运算速度,提高数据稳定性

NLLLoss(input, target)是按照target数值(一个代表一行)将input中每行对应位置的数据取出来,去掉负号,求和,再取平均。

logSoftmax + NLLLoss就是计算交叉熵

CrossEntropyLoss(input, target) = logSoftmax +NLLLoss = log(Softmax) + NLLLoss

因此,在分类问题中,要使用CrossEntropyLoss函数计算交叉熵损失,在model中最后一层就不需要添加Softmax层了。

通过实际测试确定,CrossEntropyLoss中内置的LogSoftmax默认是按照行加和为1.

参考:

https://www.zhihu.com/question/358069078

https://blog.csdn.net/qq_22210253/article/details/85229988?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

相关文章

网友评论

      本文标题:pytorch CrossEntropyLoss(), Soft

      本文链接:https://www.haomeiwen.com/subject/hkyznhtx.html