TypeError: linear(): argument 'input' (position 1) must be Tensor, not table not resolved successfully

Asked 2 years ago, Updated 2 years ago, 423 views

I would like to make an inference about the model I learned in Pythorch.
The model has two inputs and one output.
Therefore, I tried to make an inference with two inputs, but it didn't work well.
How can I move it well?

x_1=np.range (0.49, 1+2*10**-3, 2*10**-3)   
x_2 = [0.8 for i in range(len(x_1)]

                     
Convert x_test1=torch.from_numpy(x_1.astype(np.float32)).float().to(device)#x to tensor
x_test2=torch.from_numpy(np.array(x_2).astype(np.float32)).float().to(device)#x to tensor

X_test=torch.stack([x_test1, x_test2], 1).to(device)

net.eval()
a = [ ]
for i in enumerate (X_test):
    outputs = net(i)
    a.append(outputs)

Error

TypeError Traceback (most recent call last)
<ipython-input-69-78f9e90abbc3>in<module>()
      9 
     10 for i in enumerate (X_test):
--- > 11 outputs = net(i)
     12 a.append(outputs)
     13 

4 frames
/usr/local/lib/python 3.7/dist-packages/torch/nn/functional.py inline (input, weight, bias)
   1846 if has_torch_function_variadic(input, weight, bias):
   1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
->1848 return torque._C._nn.linear (input, weight, bias)
   1849 
   1850 

TypeError: linear(): argument 'input' (position 1) must be Tensor, notuple

python machine-learning pytorch

2022-09-30 22:03

1 Answers

enumerate function description in for loop is
Returns the index and object tuple.

I think the argument for the net function should be Tensor, so
If you simply want a Tensor, assume that X_Test is a list of Tensor elements, why not rewrite it as follows?

 for x in X_test:
    outputs = net(x)
    a.append(outputs) 


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.