自己研究出来了:
判断方法:
if torch.any(torch.isnan(a)):
也可以参考我的另一篇博客:
https://blog.csdn.net/jacke121/article/details/93085582
0. 人脸识别项目
卷积后,值为-inf,在后面的bn层中出现nan。
可以加个x=torch.clip(x,-1e5,1e5)
如果float16的话,会越界,可以改为:
可以加个x=torch.clip(x,-1e4,1e4)
1.分子分母同时为0的时候,会报异常NaN
import torch
a=torch.zeros(1)
b=torch.zeros(1,requires_grad=True)
print(b/0)
2.
It can be exactly zero (Anywhere the input to ReLU4
is negative, it's output will be zero).
log(0) -> NaN
During the training, the loss is Nan. The loss function is Torch.nn.CrossEntropyLoss.
Solution:
The NaNs appear, because softmax + log separ