TypeError: unsupported operand type(s) for @: 'numpy.ndarray' and 'Tensor'

TypeError: unsupported operand type(s) for @: 'numpy.ndarray' and 'Tensor'

显示标题中提到的错误。

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 数组和 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)