这里有两个与PyTorch相关的函数,分别是log_softmax和softmax。这篇PyTorch文章描述了它们之间的区别: What is the difference between log_softmax and softmax? 其中softmax函数定义为:
但是,在以下的PyTorch代码中,为什么我会得到不同的输出呢?
exp(x_i) / exp(x).sum()
,而log_softmax函数定义为:log(exp(x_i) / exp(x).sum())
。但是,在以下的PyTorch代码中,为什么我会得到不同的输出呢?
>>> it = autograd.Variable(torch.FloatTensor([0.6229,0.3771]))
>>> op = autograd.Variable(torch.LongTensor([0]))
>>> m = nn.Softmax()
>>> log = nn.LogSoftmax()
>>> m(it)
Variable containing:
`0.5611 0.4389`
[torch.FloatTensor of size 1x2]
>>>log(it)
Variable containing:
-0.5778 -0.8236
[torch.FloatTensor of size 1x2]
然而,log(0.5611)的值为-0.25095973129,而log(0.4389)的值为-0.35763441915。
为什么会出现这样的差异呢?