【问题标题】:Error: one of the variables needed for gradient computation has been modified by an inplace operation错误:梯度计算所需的变量之一已被就地操作修改
【发布时间】:2020-08-25 18:08:46
【问题描述】:

我正在为我的一个项目使用 here 可用的 Soft Actor-Critic 实现。但是当我尝试运行它时,我收到以下错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [256, 1]], which is output 0 of TBackward, is at version 3; expected version 2 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

错误来自sac.py 文件中的梯度计算。我看不到可能进行的操作。有什么帮助吗?

回溯:

Traceback (most recent call last)
<ipython-input-10-c124add9a61d> in <module>()
     22             for i in range(updates_per_step):
     23                 # Update parameters of all the networks
---> 24                 critic_1_loss, critic_2_loss, policy_loss, ent_loss, alpha = agent.update_parameters(memory, batch_size, updates)
     25                 updates += 1
     26 

2 frames
<ipython-input-7-a2432c4c3767> in update_parameters(self, memory, batch_size, updates)
     87 
     88         self.policy_optim.zero_grad()
---> 89         policy_loss.backward()
     90         self.policy_optim.step()
     91 

/usr/local/lib/python3.6/dist-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    196                 products. Defaults to ``False``.
    197         """
--> 198         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    199 
    200     def register_hook(self, hook):

/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     98     Variable._execution_engine.run_backward(
     99         tensors, grad_tensors, retain_graph, create_graph,
--> 100         allow_unreachable=True)  # allow_unreachable flag
    101 
    102 

【问题讨论】:

    标签: python pytorch reinforcement-learning


    【解决方案1】:

    只需将 PyTorch 降级为 1.5.0 下的任何内容(在撰写本文时是最新的)。

    pip uninstall torch
    pip install torch==1.4.0
    

    【讨论】:

    • 巧合的是,我在 PyTorch 从 1.4 升级到 1.5 后遇到了这个错误。 1.5 引发此错误是否有特殊原因,它是否反映了我的代码中的一些实际问题? (也就是说,如果降级 PyTorch 只是隐藏了一个实际问题,那么这不是一个很好的解决方案。)
    • @mrwheet 不知道实际问题是什么。 ... does it reflect some actual problem in my code?我非常怀疑这一点。就我而言,实际执行中没有问题。在此处和 GitHub 的大多数其他答案中,代码实际上都有一些就地操作。但正如我提到的,我找不到。我更喜欢使用 TensorFlow 而不是 PyTorch 的另一个原因。 ;)
    猜你喜欢
    • 2019-12-29
    • 2020-09-15
    • 2021-11-04
    • 2021-03-04
    • 2019-05-10
    • 2020-11-11
    • 2020-01-03
    • 2022-10-01
    • 2021-08-05
    相关资源
    最近更新 更多