我是靠谱客的博主 美丽小猫咪,这篇文章主要介绍one of the variables needed for gradient computation has been modified by an inplace项目场景:问题描述解决方案:,现在分享给大家,希望可以做个参考。

项目场景:

根据项目要求,需要使用两个优化器对模型进行优化


问题描述

运行中一直出现:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [100, 5000]], which is output 0 of TBackward, is at version 3; expected version 2 instead. Hint: enable anomaly detection

解决方案:

经过网上查询发现,这个问题可能是由于出现了x+=3类似的代码,改成x = x + 1,更改过后发现仍旧不行,后来发现,在使用两个优化器进行优化是需要是如下格式:

	 optimizerG.zero_grad() 
	 optimizerD.zero_grad() 
	 loss1.backward(retain_graph=True)  
	 loss2.backward()  
	 optimizerD.step()  
	 optimizerG.step()  

必须将参数更新放在最后

最后

以上就是美丽小猫咪最近收集整理的关于one of the variables needed for gradient computation has been modified by an inplace项目场景:问题描述解决方案:的全部内容,更多相关one内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部