版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jacke121/article/details/82932184
torch CrossEntropyLoss nan
自己研究出来了,分子分母同时为0的时候,会报异常NaN
import torch
a=torch.zeros(1)
b=torch.zeros(1,requires_grad=True)
print(b/0)
During the training, the loss is Nan. The loss function is Torch.nn.CrossEntropyLoss.
Solution:
The NaNs appear, because softmax + log separately can be a numerically unstable operation.
If you’re using CrossEntropyLoss
for training, you could use the F.log_softmax
function at the end of your model and use NLLLoss
. The loss will be equivalent, but much more stable.
转载本文请联系原作者获取授权,同时请注明本文来自高琳琳科学网博客。
链接地址:
c=torch.isnan()