RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
在我们的Train中有时不是由单个网络组成的,而是需要经过多个网络,这时便要对多个网络进行求导。一开始我是设置了两个优化器,进行了两个重复的操作。
但是!!!重复操作是不可行的!!!
在前面求导的时候要用到这个操作 “retain_graph=True” 。
如果两个模型的优化器一样,可以使用同一个,但是求导的时候一定要注意,记住更新模型参数也要放在最后。
具体示例放在下面啦~