关于torch的问题

自己在修改网络结构的时候发现如下问题 但是在网上一直找也没有找到,求指点!!
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [7594, 64]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!
python-BaseException
使用torch.autograd.set_detect_anomaly(True)进一步寻找问题 发现了
[W ..\torch\csrc\autograd\python_anomaly_mode.cpp:104] Warning: Error detected in StackBackward. Traceback of forward call that caused the error:
但是一直没有找到解决办法

img

问题大多是网络中的label和网络输出的维度大小不一样,也就是说,类别数为6类,而网络的输出为5类

你这是改了啥?计算两次loss?这个意思一般是你计算完loss,并且你的网络梯度更新完毕了,你又重新计算一次loss更新梯度就会出现这个问题