Transformation not working - tuple instead of Tensor

BlueTurtle: <p>I am getting an error when trying to run a model with the CIFAR10 dataset. I am using the included PyTorch dataset to retrieve the data and the transformation <code>transformations = transforms.ToTensor()</code> but I get the error: <code>conv2d(): argument ‘input’ (position 1) must be Tensor, not tuple</code>.</p>
<p>Here is the training loop I am using:</p>
<pre><code class="lang-auto">epochs = 1
for epoch in range(epochs):
    train_loss = 0
    val_loss = 0
    accuracy = 0
    
    # Training the model
    model.train()
    counter = 0
    for inputs, labels in train_loader:
        # Move to device
        #inputs, labels = inputs.to(device), labels.to(device)
        # Clear optimizers
        optimizer.zero_grad()
        # Forward pass
        output = model.forward(inputs)
        # Loss
        loss = criterion(output, labels)
        # Calculate gradients (backpropogation)
        loss.backward()
        # Adjust parameters based on gradients
        optimizer.step()
        # Add the loss to the training set's rnning loss
        train_loss += loss.item()*inputs.size(0)
        
        # Print the progress of our training
        counter += 1
        print(counter, "/", len(train_loader))
        
    # Evaluating the model
    model.eval()
    counter = 0
    # Tell torch not to calculate gradients
    with torch.no_grad():
        for inputs, labels in val_loader:
            # Move to device
            #inputs, labels = inputs.to(device), labels.to(device)
            # Forward pass
            output = model.forward(inputs)
            # Calculate Loss
            valloss = criterion(output, labels)
            # Add loss to the validation set's running loss
            val_loss += valloss.item()*inputs.size(0)
            
            # Since our model outputs a LogSoftmax, find the real 
            # percentages by reversing the log function
            output = torch.exp(output)
            # Get the top class of the output
            top_p, top_class = output.topk(1, dim=1)
            # See how many of the classes were correct?
            equals = top_class == labels.view(*top_class.shape)
            # Calculate the mean (get the accuracy for this batch)
            # and add it to the running accuracy for this epoch
            accuracy += torch.mean(equals.type(torch.FloatTensor)).item()
            
            # Print the progress of our evaluation
            counter += 1
            print(counter, "/", len(val_loader))
    
    # Get the average loss for the entire epoch
    train_loss = train_loss/len(train_loader.dataset)
    valid_loss = val_loss/len(val_loader.dataset)
    # Print out the information
    print('Accuracy: ', accuracy/len(val_loader))
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(epoch, train_loss, valid_loss))
</code></pre>
<p>The full error code is:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-189-55992f2f3f3c&gt; in &lt;module&gt;
     14         optimizer.zero_grad()
     15         # Forward pass
---&gt; 16         output = model.forward(inputs)
     17         # Loss
     18         loss = criterion(output, labels)

~/opt/anaconda3/envs/Test/lib/python3.7/site-packages/torchvision/models/resnet.py in forward(self, x)
    194 
    195     def forward(self, x):
--&gt; 196         x = self.conv1(x)
    197         x = self.bn1(x)
    198         x = self.relu(x)

~/opt/anaconda3/envs/Test/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--&gt; 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/opt/anaconda3/envs/Test/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
    343 
    344     def forward(self, input):
--&gt; 345         return self.conv2d_forward(input, self.weight)
    346 
    347 class Conv3d(_ConvNd):

~/opt/anaconda3/envs/Test/lib/python3.7/site-packages/torch/nn/modules/conv.py in conv2d_forward(self, input, weight)
    340                             _pair(0), self.dilation, self.groups)
    341         return F.conv2d(input, weight, self.bias, self.stride,
--&gt; 342                         self.padding, self.dilation, self.groups)
    343 
    344     def forward(self, input):

TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not tuple
</code></pre>
<p>I seem to get this quite a lot and it’s always been a sticking point no matter what dataset I use.</p>
<p>My research lead me to trying:<br>
<code>for inputs, labels in enumerate(train_loader):</code> which gave the error <code>conv2d(): argument ‘input’ (position 1) must be Tensor, not int</code></p>
<p><code>for inputs, labels in next(iter(train_loader)):</code> which gave the error <code>too many values to unpack (expected 2)</code></p>
<p>Thanks,</p>
<p>Dan.</p>

ptrblck: <p>The error message points to <code>inputs</code>, which is a <code>tuple</code>, while a tensor is expected.<br>
Check your <code>Dataset</code> and make sure that the data and target samples are returned as tensors.<br>
If you get stuck, feel free to post the code of your <code>Dataset</code>, so that we can take a look.</p>