报错信息:
one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.LongTensor [32, 128]] is at version 32; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
解决方案
加上.clone()即可
示例
我的报错代码:
x = functionA(x)
修改之后的代码:
x = functionA(x.clone())
问题原因
clone()之后的返回值才支持梯度回溯,这里是梯度传播出错
文章讲述了在使用PyTorch进行深度学习时遇到的梯度计算错误,原因是某个变量被inplace操作修改。解决方法是在涉及梯度传播的变量上使用.clone()方法创建副本。通过添加.clone(),可以确保返回的张量支持梯度回溯,从而避免错误。
3132





