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 transformations = transforms.ToTensor()
but I get the error: conv2d(): argument ‘input’ (position 1) must be Tensor, not tuple
.
Here is the training loop I am using:
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))
The full error code is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-189-55992f2f3f3c> in <module>
14 optimizer.zero_grad()
15 # Forward pass
---> 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):
--> 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:
--> 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):
--> 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,
--> 342 self.padding, self.dilation, self.groups)
343
344 def forward(self, input):
TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not tuple
I seem to get this quite a lot and it’s always been a sticking point no matter what dataset I use.
My research lead me to trying:
for inputs, labels in enumerate(train_loader):
which gave the error conv2d(): argument ‘input’ (position 1) must be Tensor, not int
for inputs, labels in next(iter(train_loader)):
which gave the error too many values to unpack (expected 2)
Thanks,
Dan.