Input type (CUDALongTensor) and weight type (CUDAFloatTensor) should be the same

hi all,
I’m new to pytorch and got in trouble testing with my pre-trained model. I think I get the input with correct format while it returns error ‘RuntimeError: Input type (CUDALongTensor) and weight type (CUDAFloatTensor) should be the same’. Here is the partial code:

net = DSN_net()
net = torch.load(args.weight)
input_data = Variable(torch.from_numpy(input_cube).long())
if args.use_gpu:
input_data = input_data.cuda()
net = net.cuda()
output = net(input_data)

also, I debug the code in this way:

input_data = Variable(torch.from_numpy(input_cube).long())
(Pdb) input_data = input_data.cuda()
(Pdb) out = net(input_data)
*** RuntimeError: Input type (CUDALongTensor) and weight type (CUDAFloatTensor) should be the same
(Pdb) type(input_data)
<class ‘torch.autograd.variable.Variable’>
(Pdb) type(net)
<class ‘dsn_net.DSN_net’>
(Pdb) type(net.conv1a.weight.data)
<class ‘torch.cuda.FloatTensor’>
(Pdb) type(input_data.data)
<class ‘torch.cuda.LongTensor’>
(Pdb) out = net(input_data)
*** RuntimeError: Input type (CUDALongTensor) and weight type (CUDAFloatTensor) should be the same

the debug results show that both the input and model weight are the desired type. In addition, I save my pre-trained model in pkl format. I have no idea why it does not work. Anyone could please help me with it ? Thanks a lot!

1 Like

Your net only takes cuda floats as input, so your data preparation does not match the expectation of later processing stages when it casts to long. The solution to that depends on what you want to achieve, leaving as float or one-hot-encoding your integer in a real vector are common things to do.

Best regards

Thomas

Thanks very much for your reply. I just changed the input_data from .long() to .float() and it works! I think I mistook the actual meaning of the error at first. Thank you again :slight_smile: