mxnet & pytorch 交叉熵损失

对比 pytorch: nn.CrossEntropyLoss()  & mxnet: gluon.loss.SoftmaxCrossEntropyLoss()

 

一般而言默认用法即可:

pytorch:

d=np.array([[1,2,3],[54,45,2],[67,4,2]])
l=np.array([0,2,1])
print(nn.CrossEntropyLoss(reduction='mean')(torch.Tensor(d),torch.Tensor(l).long()))    # 默认求均值
print(nn.CrossEntropyLoss(reduction='none')(torch.Tensor(d),torch.Tensor(l).long()))    # 不作任何处理
tensor(39.1359)
tensor([ 2.4076, 52.0001, 63.0000])

mxnet:

d=np.array([[1,2,3],[54,45,2],[67,4,2]])
l=np.array([0,2,1])
print(gluon.loss.SoftmaxCrossEntropyLoss(from_logits=False)(mx.nd.array(d), mx.nd.array(l)).mean())       # 手动求均值 
print(gluon.loss.SoftmaxCrossEntropyLoss(from_logits=False)(mx.nd.array(d), mx.nd.array(l)))              # 默认不作任何处理
[39.13591]
<NDArray 1 @cpu(0)>
[ 2.407606 52.000122 63.      ]
<NDArray 3 @cpu(0)>

猜你喜欢

转载自www.cnblogs.com/king-lps/p/13177369.html
今日推荐