当前位置:网站首页>one of the variables needed for gradient computation has been modified by an inplace

one of the variables needed for gradient computation has been modified by an inplace

2022-08-10 01:18:00 江_小_白

项目场景:

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


问题描述

运行中一直出现:
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()  

必须将参数更新放在最后

原网站

版权声明
本文为[江_小_白]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45193988/article/details/126254990