繁体   English   中英

RuntimeError:梯度计算所需的变量之一已被原位操作修改:PyTorch 错误

[英]RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: PyTorch error

我正在尝试在 PyTorch 中运行一些代码,但此时我已堆积如山:

在第一次迭代中,Discriminator 和 Generator 的反向操作都运行良好

....

self.G_loss.backward(retain_graph=True)

self.D_loss.backward()

...

在第二次迭代中,当self.G_loss.backward(retain_graph=True)执行时,我收到此错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [8192, 512]] 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!

根据torch.autograd.set_detect_anomaly ,鉴别器网络中以下最后几行负责此:

    bottleneck = bottleneck[:-1]
    self.embedding = x.view(x.size(0), -1)
    self.logit = self.layers[-1](self.embedding)

奇怪的是,我在其他代码中使用了该网络架构,它可以正常工作。 有什么建议?

完整的错误:

    site-packages\torch\autograd\__init__.py", line 127, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [8192, 512]] 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 += loss_val行的代码解决

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM