【问题标题】:RuntimeError: addmm(): argument 'mat1' (position 1) must be Variable, not torch.FloatTensorRuntimeError:addmm():参数'mat1'(位置1)必须是变量,而不是torch.FloatTensor
【发布时间】:2018-11-12 00:48:21
【问题描述】:

我按原样从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。

【问题讨论】:

    标签: python variables pytorch tensor


    【解决方案1】:

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

    **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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,所以我将添加更新命令以防您不想搜索它们:
      可选:

      conda list | grep pytorch 
      conda upgrade conda 
      

      以下更新没有更新任何东西,虽然他们应该更新,我认为这是正确的更新方式(您可以先尝试)

      conda update pytorch torchvision
      

      明确指定版本有什么帮助:

      conda install pytorch=0.4.0 -c pytorch
      

      【讨论】:

        猜你喜欢
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多