This:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) for data in dataloader: inputs, labels = data outputs = model(inputs)
Gives the error:
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
Advertisement
Answer
You get this error because your model is on the GPU, but your data is on the CPU. So, you need to send your input tensors to the GPU.
inputs, labels = data # this is what you had inputs, labels = inputs.cuda(), labels.cuda() # add this line
Or like this, to stay consistent with the rest of your code:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") inputs, labels = inputs.to(device), labels.to(device)
The same error will be raised if your input tensors are on the GPU but your model weights aren’t. In this case, you need to send your model weights to the GPU.
model = MyModel() if torch.cuda.is_available(): model.cuda()