Argument must be tensor not tuple

Hello,
i would like to get some advice about what am i doing wrong.
I loaded data and created the tensors, and then i´m using dataloader.
When i want to make forward with my CNN, i get this error:
TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not tuple
Conv2d is the first layer in the network. I also printed my input for model in forward function and there is tensor.
There is this:

tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          ...,
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.]],

         [[0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          ...,
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.]],

         [[0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          ...,
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.]]]])

My network class looks like this:

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.features=getModel()
    def forward(self,x):
        print(x)
        x=self.features(x)
        return x

and in my method getModel() i`m using nn.sequential,

With no information about the DataLoader as well as the way you are feeding your batches to your model during training, it’s really difficult to say. But one hypothesis could be that if you are feeding your samples X and labels y to the DataLoader, it is most likely returning a tuple of (X, y) which you may be feeding directly (without splitting) into the model during training. I hope this makes sense to you.

This is my dataloader class:

class Dataset(torch.utils.data.Dataset):
  'Characterizes a dataset for PyTorch'
  def __init__(self, list_IDs, tensorDict,GTDict):
        'Initialization'
        self.list_IDs = list_IDs
        self.tensorDict=tensorDict
        self.GTDict=GTDict
  def __len__(self):
        'Denotes the total number of samples'
        return len(self.list_IDs)

  def __getitem__(self, index):
        'Generates one sample of data'
        # Select sample
        key = self.list_IDs[index]

        # Load data and get label
        X = torch.load(self.tensorDict[key])
        y = cv2.imread(self.GTDict[key])
        return X, y

So yes, i’m returning a tuple of x,y but then i split it also

training_set = Dataset(listIDs['train'],tensors['train'],groundTruth['train'])
training_generator = torch.utils.data.DataLoader(training_set, **params['train'],batch_size=1)
while(stopTraining):
    for inputForNetwork,outputFromNetwork in training_generator:
        out=model.forward(inputForNetwork)

i printed it out before i use inputForNetwork in network Forward() method, the output is at my previous post,

First i did not used batch_size=1 but then i was receiving error that i need 4 dimensions not 3

i’m loading my tensors like this:

    statsAboutFile=[]
    for typeOfStatistics in parameters.stats:
        fullNameOfFile=shortenedNameOfFile+typeOfStatistics+".csv"
        dataOriginal=pd.read_csv(fullNameOfFile, index_col =False,header = None).astype('float')
        torch_tensor=torch.tensor(dataOriginal.values)
        torch_tensor=torch_tensor.float()
        statsAboutFile.append(torch_tensor)
    return torch.stack(statsAboutFile)

then with this returned i use only torch.save(), and then torch.load() in getitem()

Are you sure your code is failing at the first layer or any other line with a Conv2d layer preceded by a Pool2d?

1 Like

i used pool with indices set to true in nn sequential, that was the problem :slight_smile: