我正在运行来自 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 = 变量(torch.randn(N,D_in)) y = Variable(torch.randn(N, D_out))
但是得到
AttributeError: 'Variable' 对象没有属性 'item'
所以这没有帮助。
我正在运行 PyTorch 版本 0.3.1.post2。
请您参考如下方法:
我想我刚刚找到了我自己问题的答案,所以如果其他人遇到这个问题,我会把它留在这里:
**注意:**这些示例已针对 PyTorch 0.4 进行了更新,对核心 PyTorch API 进行了多项重大更改。最值得注意的是,在 0.4 之前,必须将 Tensors 包装在 Variable 对象中才能使用 autograd;此功能现在已直接添加到 Tensors,现在已弃用变量。
所以这意味着我正在运行旧版本的 PyTorch
