RuntimeError:addmm():参数'mat1'(位置1)必须是Variable,而不是torch.FloatTensor

时间:2018-05-22 19:23:51

标签: python variables pytorch tensor

我从the documentation按原样运行这个非常简单的PyTorch示例NN,没有任何改变。

我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
    input = module(input)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py", line 835, in linear
    return torch.addmm(bias, input, weight.t())
RuntimeError: addmm(): argument 'mat1' (position 1) must be Variable, not torch.FloatTensor

显然在矩阵乘法过程中,存在一些数据类型错误 为什么我试图乘法的矩阵需要变量呢?

我能做到 x = Variable(torch.randn(N, D_in)) y = Variable(torch.randn(N, D_out))
但是得到了 AttributeError: 'Variable' object has no attribute 'item'
所以没有帮助。

我正在运行PyTorch版本0.3.1.post2。

2 个答案:

答案 0 :(得分:3)

我想我刚刚找到了自己问题的答案,所以如果有其他人遇到这个问题,我会把它留在这里:

**NOTE:** These examples have been update for PyTorch 0.4, which made several major changes to the core PyTorch API. Most notably, prior to 0.4 Tensors had to be wrapped in Variable objects to use autograd; this functionality has now been added directly to Tensors, and Variables are now deprecated.

所以这意味着我正在运行旧版本的PyTorch

答案 1 :(得分:3)

我有同样的问题,所以我只是添加更新命令,以防您不想搜索它们:
可选

conda list | grep pytorch 
conda upgrade conda 

以下更新没有更新任何内容,尽管它们应该并且我认为它是正确的更新方式(您可以先尝试一下)

conda update pytorch torchvision

明确指定版本的帮助是什么:

conda install pytorch=0.4.0 -c pytorch