admin管理员组文章数量:1608852
问题描述:
在PyTorch框架下搭建简单的网络结构并训练时,报以下错误:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.DoubleTensor [100, 300]], 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!
这个错误产生的原因是,和loss反向传播梯度计算有关的变量在反向传播前通过inplace操作被修改了。inplace操作包括如:x += 1、x[0] = 1、torch.add_()等。
出现这个问题可以检查下:
1、forward()函数中,是否在输出计算后又修改了和输出计算有关的变量。
因为输出和loss的计算有关,在通过某些变量计算得到输出后,就不能再通过inplace操作修改这些变量了。(可以通过.clone()或者另赋一个变量来修改)
比如:
代码例来自:https://github/pytorch/pytorch/issues/15803
b = a ** 2 * c ** 2
b +&
本文标签: inplacePytorchRuntimeErroroperation
版权声明:本文标题:Pytorch: inplace operation runtimeError 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728550760a1163402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论