Previously I have learned that the softmax as the output layer coupled with the log-likelihood cost function (the same as the the nll_loss in pytorch) can solve the learning slowdown problem.

However, while I am learning the pytorch mnist tutorial, I'm confused that why the combination of the log_softmax as the output layer and the nll_loss(the negative log likelihood loss) as the loss function was used (L26 and L34).

I found that when log_softmax+nll_loss was used, the test accuracy was 99%, while when softmax+nll_loss was used, the test accuracy was 97%.

I'm confused that what's the advantage of log_softmax over softmax? How can we explain the performance gap between them? Is log_softmax+nll_loss always better than softmax+nll_loss?


1 Answer 1


The short answer is yes, log_softmax + nll_loss will work better.

I don’t know the implementation details under the hood in PyTorch, but see the screenshot below from the documentation:


  • $\begingroup$ Yes, I know that log_softmax + nll_loss will work better, but I want to know why... $\endgroup$
    – user1024
    May 1, 2019 at 3:54
  • $\begingroup$ @user1024 this seems like a question for the development team since it could depend heavily on their implementation. But you mentioned you used softmax + nll_loss together. Note that since log is monotonically increasing, maximizing the log of the probability with respect to theta is the same as maximizing the probability with respect to theta. That is they both have the same argmax. But log likelihood expects log probabilities, this log_softmax is used. Also note that log(a/b) = log(a) - log(b). But since b is the same for all classes, it’s (probably) omitted in their implementation. $\endgroup$
    – Hanzy
    May 1, 2019 at 4:33
  • $\begingroup$ @user1024 omitted because it wouldn’t affect the maximization, so I assume it would make it faster and more numerically stable. But I also assume they have further optimized the routine implementation in ways that aren’t as obvious simply for speed and stability performance. $\endgroup$
    – Hanzy
    May 1, 2019 at 4:34
  • $\begingroup$ @user1024 and assuming that we are using a Gaussian prior, then maximizing the likelihood requires us to take an exponentiation, but taking the log cancels out the exponentiation, which would also make it faster and more stable. $\endgroup$
    – Hanzy
    May 1, 2019 at 4:36
  • $\begingroup$ Thank you @Hanzy, your comments helped explain the advantages of log_softmax in speed and numerical properties. As a beginner in AI, I have allodoxaphobia to determine so many super-parameters. $\endgroup$
    – user1024
    May 4, 2019 at 3:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .