Hello lidaiyu!
You most likely have two separate issues here.



 lidaiyu:

When I using

Blockquote ```
x = F.softmax(x, dim=1)
x = torch.log(x)

to replace

> Blockquote ```
x = F.log_softmax(x, dim=1)

something wrong happend.


log (softmax()) can have numerical issues.  That’s why
you are better off using log_softmax().  It is mathematically
the same, but numerically different, and, in fact, numerically
more stable.


Then I subtracted the former and the latter, and found that they were not exactly the same.


However, the result of the subtraction that you posted in your
screenshot (Note, please post text instead of screenshots for
textual information.) shows that the two differ only by a small
round-off error.  This is to be expected.
Try converting your tensor a to DoubleTensor
(dtype = torch.float64) before performing the rest of
the computation, and the discrepancy will get smaller (although
not necessarily go to zero).


I need the result of “F.softmax”.


My assumption is that you are using log_softmax() for training,
but need softmax() for some other purpose.  If this is the case,
keep using log_softmax() for training, and compute softmax()
separately.  (Or you can re-exponentiate log_softmax() to
recover softmax().)
Good luck.
K. Frank