Expected object of scalar type Long but got scalar type Float for argument #2 'mat2'


(srikar ym) #1

Using torch.nn’s GRU gives the error Expected object of scalar type Long but got scalar type Float for argument #2 ‘mat2’. I converted the input before passing it to the gru and also printed its type() to verify that it is a Long tensor.

in1 = x.unsqueeze(0).type(torch.LongTensor).
in2 = (hxs * masks).unsqueeze(0).type(torch.LongTensor).
print(in1.type(),in2.type()) #Gives torch.cuda.LongTensor
x, hxs = self.gru(in1, in2)

The gru is defined as self.gru = nn.GRU(recurrent_input_size, hidden_size).cuda()


(Yunusemre) #2

Have you checked your weights? Initially Pytorch Tensors have Float type and it will cause problems when you want to use different type of tensors. For example:

2019-01-08%2020-54-20%20ekran%20g%C3%B6r%C3%BCnt%C3%BCs%C3%BC

For solution, you should change your layer weight type. Also you can set default tensor type if you are using all LongTensor:

torch.set_default_tensor_type('torch.cuda.LongTensor')