Argument must be tensor not tuple

Michal_Zelenak: <p>Hello,<br>
i would like to get some advice about what am i doing wrong.<br>
I loaded data and created the tensors, and then i´m using dataloader.<br>
When i want to make forward with my CNN, i get this error:<br>
TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not tuple<br>
Conv2d is the first layer in the network. I also printed my input for model in forward function and there is tensor.<br>
There is this:</p>
<pre><code class="lang-auto">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.]]]])

</code></pre>
<p>My network class looks like this:</p>
<pre><code class="lang-auto">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

</code></pre>
<p>and in my method getModel() i`m using nn.sequential,</p>

harsha_g: <p>With no information about the <code>DataLoader</code> 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 <code>X</code> and labels <code>y</code> to the <code>DataLoader</code>, it is most likely returning a tuple of <code>(X, y)</code> which you may be feeding directly (without splitting) into the <code>model</code> during training. I hope this makes sense to you.</p>

Michal_Zelenak: <p>This is my dataloader class:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>So yes, i’m returning a tuple of x,y but then i split it also</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>i printed it out before i use <code>inputForNetwork</code> in network <code>Forward()</code> method, the output is at my previous post,</p>
<p>First i did not used <code>batch_size=1</code> but then i was receiving error that i need 4 dimensions not 3</p>
<p>i’m loading my tensors like this:</p>
<pre><code class="lang-auto">    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)
</code></pre>
<p>then with this returned i use only <code>torch.save()</code>, and then <code>torch.load()</code> in <code>getitem()</code></p>

harsha_g: <aside class="quote no-group" data-username="Michal_Zelenak" data-post="1" data-topic="85465">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/michal_zelenak/48/17896_2.png" class="avatar"> Michal_Zelenak:</div>
<blockquote>
<p>TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not tuple</p>
</blockquote>
</aside>
<p>Are you sure your code is failing at the first layer or any other line with a <code>Conv2d</code> layer preceded by a <code>Pool2d</code>?</p>

Michal_Zelenak: <p>i used pool with indices set to true in nn sequential, that was the problem <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>