【问题标题】:TypeError: unsupported operand type(s) for @: 'numpy.ndarray' and 'Tensor'类型错误:@ 不支持的操作数类型:“numpy.ndarray”和“张量”
【发布时间】:2021-10-15 13:50:35
【问题描述】:

显示标题中提到的错误。

TypeError: @: 'numpy.ndarray' 和 'Tensor' 的操作数类型不受支持

使用 Ubuntu 和 VS Studio Code(新手)。

使用下面的代码:-

import numpy as np
import torch

inputs = np.array([[73, 67, 43], 
                   [91, 88, 64], 
                   [87, 134, 58], 
                   [102, 43, 37], 
                   [69, 96, 70]], dtype='float32')

targets = np.array([[56, 70], 
                    [81, 101], 
                    [119, 133], 
                    [22, 37], 
                    [103, 119]], dtype='float32')

w = torch.randn(2,3, requires_grad= True)
b = torch.randn(2, requires_grad = True)

print(w)
print(b)```

Works till here!

```def model(x):
    return (x@w.t()+ b)

preds = model(inputs)
print(preds)```


Following the tutorial (But using VS Studio Code): https://jovian.ai/aakashns/02-linear-regression
#NewUser #Ubuntu #VSStudioCode

Intuition: The error is with '@' operator of PyTorch. Tried creating Tensors, working fine.

【问题讨论】:

  • 把你的 numpy 数组转成 Tensor 应该没问题
  • 有效。谢谢。

标签: python numpy ubuntu visual-studio-code torch


【解决方案1】:

在计算操作时,您应该坚持使用一个框架:您不能在同一操作中使用 NumPy 数组和 Torch 张量(即x@w.t())。

您可以将输入和输出实例化为张量:

inputs = torch.tensor([[ 73,  67,  43], 
                       [ 91,  88,  64], 
                       [ 87, 134,  58], 
                       [102,  43,  37], 
                       [ 69,  96,  70]], dtype=float)

targets = torch.tensor([[ 56,  70], 
                        [ 81, 101], 
                        [119, 133], 
                        [ 22,  37], 
                        [103, 119]], dtype=float)

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 2013-04-26
    • 1970-01-01
    • 2013-01-02
    • 2015-12-11
    • 1970-01-01
    • 2017-05-27
    • 2014-06-15
    • 2012-11-01
    相关资源
    最近更新 更多