RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results

报错 RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already bee

报错 RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling .backward() the first time.

去网上搜了一下,一般有三种情况:

  1. 训练模型中有多个损失函数导致的,所以按照报错提示的内容在前面的几个(除了最后一个)loss.backward() 的括号里加上retain_graph=True,就解决了。
  2. 计算图中有环,循环依赖,可以用 detach() 解决。
  3. 特定的网络会出现这种问题,比如GAN、RNN等,参考其他博客。

我的情况不属于上面三种。是在模型的 init 中定义了一个二维的 vector,并使用了 F.normalize(),错就错在这。把 F.normalize() 用在 forward() 里,而不是 init 里,就解决了。因为啥我也不知道,希望大神给我讲讲。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755027593a5228488.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信