RuntimeError: one of the variables needed for gradient computation has been modified by an inplace

具体问题如下:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [2, 64, 96, 2]], which is output 0 of SelectBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

解决方法如下:

首先定位问题,根据提示[torch.FloatTensor [2, 64, 96, 2]], 我通过debug定位问题的位置 找到如下的代码片段:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace_第1张图片

然后整个代码有五处这类型的代码 所以都有可能导致这类的问题存在

因此根据大佬的建议 进一步定位问题的位置 搞清楚到底哪部分出问题,然后有了一下的代码去替换上述的位置的代码,准确定位问题:

 RuntimeError: one of the variables needed for gradient computation has been modified by an inplace_第2张图片

然后定位得到确实出问题的四处代码,之后寻找方法解决inplace的问题,找到如下的链接:

 PyTorch中的拷贝和就地操作总结_Chris_34的博客-CSDN博客_pytorch拷贝

之后修改代码解决问题;

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace_第3张图片

 

总结:总的一个过程为,发现问题,定位问题,解决问题,最终的解决方案如上所示。

你可能感兴趣的