【问题标题】:What is the difference between softmax and log-softmax?softmax 和 log-softmax 有什么区别?
【发布时间】:2018-03-12 13:34:38
【问题描述】:

这两个函数之间的区别已经在这篇 pytorch 帖子中描述过:What is the difference between log_softmax and softmax? 是: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

为什么会有这样的差异?

【问题讨论】:

    标签: machine-learning deep-learning pytorch softmax


    【解决方案1】:

    torch.log默认提供输入的自然对数,所以PyTorch的输出是正确的:

    ln([0.5611,0.4389])=[-0.5778,-0.8236]
    

    您最后的结果是使用以 10 为底的对数获得的。

    【讨论】:

      【解决方案2】:

      不仅默认,torch.log 始终是自然对数。 而torch.log10 是以 10 为底的日志。

      【讨论】:

        猜你喜欢
        • 2016-03-18
        • 1970-01-01
        • 2021-01-13
        • 2013-07-29
        • 2016-03-04
        • 2013-07-25
        • 1970-01-01
        • 2016-07-03
        • 1970-01-01
        相关资源
        最近更新 更多