【报错】: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 - 博客园 

你可能感兴趣的:(深度学习,神经网络,人工智能)