当前位置: 代码迷 >> 综合 >> pytorch 报错:RuntimeError: one of the variables needed for gradient computation has been modified
  详细解决方案

pytorch 报错:RuntimeError: one of the variables needed for gradient computation has been modified

热度:39   发布时间:2024-03-09 15:40:18.0

完整错误

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 3, 512, 512]] is at version 7; 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).

检查了好久,发现是代码不规范。
在调用函数时候,使用了这样的操作
x1 = self.build_fine2coarse(x1+aux_f)

改成这样就好了
x = x1+aux_f
x1 = self.build_fine2coarse(x)

这个错误,归根结底,就是避免inplace操作

  相关解决方案