I’m trying to train a network. I have set the device to “cuda”, and I run
However, when I run
inputs = data["input"].float().to(device)
outputs = net(inputs)
I get an error at the outputs assignment.
“RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #4 ‘mat1’”
Puzzlingly enough, with
inputs = data["input"].float()
outputs = net(inputs)
I get, erring on the same last line of code, the “opposite error”:
“RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 ‘mat1’”
However, if I remove all “to(device)” then the code runs fine.
What might be going on here?
Which version of pytorch are you using?
It will be great if you could give a short reproducible script.
This works for me. Does it work for you?
device = 'cuda'
x = torch.rand(5,5)
y = x.to(device)
import torch.nn as nn
net = nn.Linear(5,5)
out = net(y)
I’m using version 0.4.1.
Your example did work for me. I’ll try to create a minimal reproducible script as soon as I can.
However, I also wonder in generally what dictates the kind of input that a custom defined net is expecting. In your case, while you saved a layer in a variable called “net”, it’s not of the Module class, which I suspect might be relevant.
Is sending a net “to(device)” which is cuda an if and only if condition for it to expect cuda input?
Just for the sake of archival conclusion I’ll say that I went back to the same code, and I managed to run it with Cuda. Apparently I just didn’t append".to(device)" to all necessary variables.
I’d say the error messages were not so informative, but I think that’s a known issue already (I remember reading people mentioning it, probably on this here forum). Perhaps it was solved on the 1.0 version which I had not the pleasure yet to try.
Thank you for the help!
@Konotori About Arul’s answer, layer is actually a subclass of
Module, so it is relevant.
May I ask what variable you missed to append
.to(device) to? It seems you already get the input and model on cuda.
Yes, I noticed that what he defined as a net was really only a layer.
As for your question, the extra
.to(device) I added was to the labels.