报错代码
self.loss_G_latent = (self.latent_i.mean() - self.latent_t.mean()) * self.opt.lambda_A
self.loss_G_latent.backward()
运行到最后一行时报错:
*** RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
其中,latent_i和latent_t都是网络中间latent layer的输出。
这一错误的原因还没有搞清楚,大概好像是latnet_t和latent_i都是网络中间层的输出,所以emmmmmG反向传播的时候就会出问题???
真的没有搞明白,下面这篇文章说的比较清楚:
https://zhuanlan.zhihu.com/p/38475183
但经过尝试找到了如下解决方案:
self.loss_G_latent = (self.latent_i.mean().detach() - self.latent_t.mean().detach()) * self.opt.lambda_A
self.loss_G_latent.backward()
即可解决报错。https://github.com/pytorch/pytorch/issues/6990
这里的detach返回了一个新的从当前图中分离的 Variable,且返回的 Variable 永远不会需要梯度。
然而为什么就解决报错了呢?
但无论如何使用detach的确能够避免类似的in-place报错,下次有时间再去追究原因。
更新:
上述方法的确可以解决报错,但不可取;
一旦使用detach则latent_i和latent_t将不具备梯度,无法反向传播,导致网络不收敛!
建议采取的方案是建立子网络,避免求梯度时发生前面参数的改变导致梯度计算错误从而报错。