报错提示:RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [128, 1]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
解决方法:将图3中的opt_D.step() 放到倒数第二行,即opt_G.step()前面。然后就解决了,有类似问题的,可能就是第一次的梯度更新导致一些参数变了。
个人亲试:相同的结构,改变一下step的位置就可以了
参考博客:愿问题解决方案