中装建设集团网站,南京做网站优化,网站建设中的财务预算,网店管家在网上查了一些资料#xff0c;但是这个情况和网上都不太一样。前100epoch能正常训练#xff0c;loss缓慢下降#xff0c;精度缓慢增大#xff0c;但是突然loss就Nan了#xff0c;我想应该不是样本问题也不是梯度爆炸或者loss中有除0吧#xff0c;毕竟都训练了100epoch了…在网上查了一些资料但是这个情况和网上都不太一样。前100epoch能正常训练loss缓慢下降精度缓慢增大但是突然loss就Nan了我想应该不是样本问题也不是梯度爆炸或者loss中有除0吧毕竟都训练了100epoch了 最终发现问题 之前代码为
predict torch.log(torch.softmax(result, dim-1))损失函数为
torch.nn.NLLLOSS更改后
#predict torch.log(torch.softmax(result, dim-1))直接删去softmax和log而损失函数改为
criterion nn.CrossEntropyLoss()nan消失 网上查阅nn.CrossEntropyLoss()的实现为
import torch.nn as nnm nn.LogSoftmax()loss nn.NLLLoss()# input is of size nBatch x nClasses 3 x 5input autograd.Variable(torch.randn(3, 5), requires_gradTrue)# each element in target has to have 0 value nclassestarget autograd.Variable(torch.LongTensor([1, 0, 4]))output loss(m(input), target)其实直接使用pytorch中的loss_funcnn.CrossEntropyLoss()计算得到的结果与softmax-log-NLLLoss计算得到的结果是一致的。那原因主要在nn.LogSoftmax()上了。直接使用nn.LogSoftmax()和分开写torch.log(torch.softmax(result, dim-1))有什么不一样吗?为什么torch.log(torch.softmax(result, dim-1))这样写会在训练过程中产生nan呢