RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
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()
Here is the documentation for
cpu(), its opposite.
The new API is to use
The advantage is obvious and important. Your device may tomorrow be something other than "cuda":
So try to avoid
It is not wrong to check for the device
dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
or to hardcode it:
In general you can use this code:
model.to(dev) data = data.to(dev)