python - 无法弄清楚 pytorch 代码中的就地操作?

标签 python machine-learning pytorch lstm recurrent-neural-network

我在 PyTorch 中有以下实现,用于使用 LSTM 进行学习:

https://gist.github.com/rahulbhadani/f1d64042cc5a80280755cac262aa48aa

但是,代码遇到就地操作错误

我的错误输出是:

/home/ivory/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  # Remove the CWD from sys.path while we load stuff.
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-86-560ec78f2b64> in <module>
     27 linear = torch.nn.Linear(hidden_nums, output_dim)
     28 
---> 29 global_loss_list = global_training(lstm2)

<ipython-input-84-152890a3028c> in global_training(optimizee)
      3     adam_global_optimizer = torch.optim.Adam([{'params': optimizee.parameters()}, 
      4                                      {'params':linear.parameters()}], lr = 0.0001)
----> 5     _, global_loss_1 = learn2(LSTM_Optimizee, training_steps, retain_graph_flag=True, reset_theta=True)
      6 
      7     print(global_loss_1)

<ipython-input-83-0357a528b94d> in learn2(optimizee, unroll_train_steps, retain_graph_flag, reset_theta)
     43             # requires_grad=True. These are accumulated into x.grad for every
     44             # parameter x. In pseudo-code: x.grad += dloss/dx
---> 45             loss.backward(retain_graph = retain_graph_flag) #The default is False, when the optimized LSTM is set to True
     46 
     47             print('x.grad: {}'.format(x.grad))

~/anaconda3/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    116                 products. Defaults to ``False``.
    117         """
--> 118         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    119 
    120     def register_hook(self, hook):

~/anaconda3/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     91     Variable._execution_engine.run_backward(
     92         tensors, grad_tensors, retain_graph, create_graph,
---> 93         allow_unreachable=True)  # allow_unreachable flag
     94 
     95 

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

我试图追踪错误,但没有成功。在这方面的任何帮助将不胜感激。

谢谢。

最佳答案

我认为问题在于以下行:

global_loss_list.append(global_loss.detach_())

PyTorch 中就地操作的惯例是在函数名称末尾使用 _ (如 detach_ 中所示)。我相信你不应该就地分离。换句话说,将 detach_ 更改为 detach

关于python - 无法弄清楚 pytorch 代码中的就地操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500582/

相关文章:

python - Apache Spark 使用的 python 版本

python - 使用 pyserial 将数据发送到串行 - 在 Razor 9DOF IMU 上工作

r - 在 R 中绘制决策边界

python - 如何在 Pytorch/Python 中实现多项式回归

python - 将 scipy coo_matrix 转换为 pytorch 稀疏张量

dataset - torch中default_loader的作用是什么?

python - 函数调用中 *args 的执行模型是什么?

python - 磁盘空间 - Python 字典与列表

nlp - 为什么使用 MALLET 主题推断对单个文档和批量文档会得到不同的结果?

python - "ValueError: Expected 2D array, got 1D array instead"将数据拟合到模型中时