admin管理员组文章数量:1608851
有一段代码在 pytorch 1.2 上没有问题,但是移植到 pytorch 1.8 就会报如下错误:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3136, 10]], which is output 0 of TBackward, 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!
经过检查,代码里并没有用到 inplace 操作。
后来发现这是 pytorch 版本更新造成的,对于 pytorch 1.4 之前的版本,如下代码是不会出错的:
opt1.zero_grad()
loss1.backward()
opt1.step()
opt2.zero_grad()
loss2.backward()
opt2.step()
但是更新到 pytorch 1.5 之后,这种操作就会报错,应该用下面代码代替:
opt1.zero_grad()
loss1.backward()
opt2.zero_grad()
loss2.backward()
opt1.step()
opt2.step()
这个BUG难了我一下午,特此记录一下。
本文标签: variablesNeededPytorchBugmodified
版权声明:本文标题:PyTorch Bug 记录:one of the variables needed for gradient computation has been modified by an inplace 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728550977a1163430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论