我是靠谱客的博主 妩媚白猫,这篇文章主要介绍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定位问题的位置 找到如下的代码片段:

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

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

 

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

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

之后修改代码解决问题;

 

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

最后

以上就是妩媚白猫最近收集整理的关于RuntimeError: one of the variables needed for gradient computation has been modified by an inplace的全部内容,更多相关RuntimeError:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部