AttributeError: 'list' object has no attribute 'dim' when predicting in pytorch
10,261
It looks like your X
(data
) is a list of tensors, while a PyTorch tensor is expected.
Try X = torch.stack(X).to(device)
before sending to the model.
Related videos on Youtube
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I'm currently loading in a model and 11 input values. Then I'm sending those 11 values into a tensor and attempting to predict outputs. Here is my code:
# coding: utf-8 # In[5]: import torch import torchvision from torchvision import transforms, datasets import torch.nn as nn import torch.nn.functional as F import torch.utils.data as utils import numpy as np data_np = np.loadtxt('input_preds.csv', delimiter=',') train_ds = utils.TensorDataset(torch.tensor(data_np, dtype=torch.float32).view(-1,11)) trainset = torch.utils.data.DataLoader(train_ds, batch_size=1, shuffle=True) # setting device on GPU if available, else CPU, replace .cuda() with .to(device) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') class Net(nn.Module): def __init__(self): super().__init__() #self.bn = nn.BatchNorm2d(11) self.fc1 = nn.Linear(11, 22) self.fc2 = nn.Linear(22, 44) self.fc3 = nn.Linear(44, 22) self.fc4 = nn.Linear(22, 11) def forward(self, x): #x = x.view(-1, 11) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) #return F.log_softmax(x, dim=1) return x model1 = torch.load('./1e-2') model2 = torch.load('./1e-3') for data in trainset: X = data X = X output = model1(X).to(device) print(output)
However, I get this error
Traceback (most recent call last): File "inference.py", line 53, in <module> output = model1(X).to(device) File "C:\Users\Happy\Miniconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__ result = self.forward(*input, **kwargs) File "inference.py", line 40, in forward x = F.relu(self.fc1(x)) File "C:\Users\Happy\Miniconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__ result = self.forward(*input, **kwargs) File "C:\Users\Happy\Miniconda3\envs\torch\lib\site-packages\torch\nn\modules\linear.py", line 55, in forward return F.linear(input, self.weight, self.bias) File "C:\Users\Happy\Miniconda3\envs\torch\lib\site-packages\torch\nn\functional.py", line 1022, in linear if input.dim() == 2 and bias is not None: AttributeError: 'list' object has no attribute 'dim'
I've tried to convert the batch to a numpy array but that didn't help. How do I resolve this error? Thank you for your help.
-
Admin over 4 yearsThank you for responding! I tried implementing your suggestion, and I got this error: Traceback (most recent call last): File "inference.py", line 45, in <module> X = torch.Tensor(X) ValueError: only one element tensors can be converted to Python scalars
-
Admin over 4 yearsI get a new error. RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 'mat2'
-
Sergii Dymchenko over 4 yearsLooks like the mode is on GPU, need to put data on GPU also:
X = torch.stack(X).to(device)
-
Admin over 4 yearsSorry about that silly mistake. I'm used to the tensorflow programming, where you don't need to send data to the GPU.