【报错】:RuntimeError: Trying to backward through the graph a second time, but the saved intermediate re

错误问题:

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() or autograd.grad() the first time.

这个错误,一般是训练模型中有多个(两个或以上)的损失函数计算,导致的,所以在第一个loss.backward()加上retain_graph=True

即:loss.backward(retain_graph=True)

但是针对于我的问题,我的模型类似GAN模型的

所以即使加上还是会报错:

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

解决办法:

在鉴定模型和生成模型之间要在使用一次鉴定模型

参考博客:

python - One of the variables modified by an inplace operation - Stack Overflow

pytorch中反向传播的loss.backward(retain_graph=True)报错 - StarZhai - 博客园 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值