PyTorch出现如下报错:RuntimeError: one of the variables needed for gradient computation has been modified b

报错内容如下:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3, 3, 1, 1]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that fail

查询网上内容是,说吧inplace设置为False,仍然解决不了,直接去问ChatGPT,给出的解释是:

于是在代码中添加了这句代码: 

    torch.autograd.set_detect_anomaly(True)

于是给出了具体错误的地方:

D:\ANACONDA\envs\matpy\lib\site-packages\torch\autograd\__init__.py:197: UserWarning: Error detected in ConvolutionBackward0. Traceback of forward call that caused the error:
  File "D:/打工人专属/STUDY/python/sonic/SignalTest/Uetpp_4output/train_avg_fenbieTiaocan_OldRes.py", line 292, in <module>
    outputs = SncNet(Sonics)
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\打工人专属\STUDY\python\sonic\SignalTest\Uetpp_4output\Unetpp_4output_OldRes.py", line 177, in forward
    f4 = self.final_3(x0_2)
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\nn\modules\container.py", line 204, in forward
    input = module(input)
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\nn\modules\conv.py", line 463, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\nn\modules\conv.py", line 459, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\fx\traceback.py", line 57, in format_stack
    return traceback.format_stack()
 (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\autograd\python_anomaly_mode.cpp:119.)
  Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
Traceback (most recent call last):
  File "D:/打工人专属/STUDY/python/sonic/SignalTest/Uetpp_4output/train_avg_fenbieTiaocan_OldRes.py", line 298, in <module>
    singleLoss.backward()  # 反向传播
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\_tensor.py", line 488, in backward
    torch.autograd.backward(
  File "D:\ANACONDA\envs\matpy\lib\site-packages\torch\autograd\__init__.py", line 197, in backward
    Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3, 3, 1, 1]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

进程已结束,退出代码1

出错原因很简单,就是我重复引用了一个卷积层,简单修改后,运行通过!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年11月13日
下一篇 2023年11月13日

相关推荐