Why can't a LongTensor take in FloatTensor in it's constructor

a = Variable(torch.LongTensor(torch.rand(2, 3)))

The error:

TypeError                                 Traceback (most recent call last)
<ipython-input-12-f8802b707ca7> in <module>()
----> 1 a = Variable(torch.LongTensor(torch.rand(2, 3)))

TypeError: torch.LongTensor constructor received an invalid combination of arguments - got (torch.FloatTensor), but expected one of:
 * no arguments
 * (int ...)
      didn't match because some of the arguments have invalid types: (torch.FloatTensor)
 * (torch.LongTensor viewed_tensor)
      didn't match because some of the arguments have invalid types: (torch.FloatTensor)
 * (torch.Size size)
      didn't match because some of the arguments have invalid types: (torch.FloatTensor)
 * (torch.LongStorage data)
      didn't match because some of the arguments have invalid types: (torch.FloatTensor)
 * (Sequence data)
      didn't match because some of the arguments have invalid types: (torch.FloatTensor)

Why is not there a conversion?Is it just a code style, or just a little bug?

Iā€™m not sure why you are trying to Construct a LongTensor from a FloatTensor (we do not support this in the constructor).

Instead, a much simpler and equivalent way:

a = Variable(torch.rand(2, 3).long())
1 Like