欢迎关注我的公众号:
本篇博客主要解决Torch在运行过程中出现的如下问题:
Traceback (most recent call last):
File "D:/Python27/Lib/site-packages/django/bin/pytorchlearn/name_class.py", line 133, in <module>
output, next_hidden = rnn(input[0], hidden)
File "D:\Users\Seavan_CC\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 206, in __call__
result = self.forward(*input, **kwargs)
File "D:/Python27/Lib/site-packages/django/bin/pytorchlearn/name_class.py", line 106, in forward
hidden = self.i2h(combined) # 计算隐藏层参数
File "D:\Users\Seavan_CC\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 206, in __call__
result = self.forward(*input, **kwargs)
File "D:\Users\Seavan_CC\Anaconda3\lib\site-packages\torch\nn\modules\linear.py", line 54, in forward
return self._backend.Linear.apply(input, self.weight, self.bias)
RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn't satisfy this condition
解决方案:
导入torch.autograd.Variable,并使用Variable将输入变量转换为input tensor,具体代码如下所示:
将
output, next_hidden = rnn(input, hidden)
转换为
from torch.autograd import Variable
output, next_hidden = rnn(Variable(input, Variable(hidden))