Pytorch Tensor type casting

In Python3 & PyTorch 1.0.0,

torch.LongTensor and torch.cuda.LongTensor means int64.

HalfTensor : float16
FloatTensor : float32
DoubleTensor : float64
ByteTensor : uint8(unsigned)
CharTensor : int8(signed)
ShortTensor : int16(signed)
IntTensor : int32(signed)
LongTensor : int64(signed)

One example of conversion from LongTensor to FloatTensor:

1
2
a = torch.LongTensor(22)
a = a.float()

Attention:

1
2
3
a = torch.LongTensor(22)
b = 100. * a

b.type() equals LongTensor. The implicit type casting did not work because type(a) is torch.Tensor instead of Python raw numbers or Numpy array.
The solution is as follows:

1
2
a = torch.LongTensor(22)
b = 100 * a.float()

Here, b.type() equals FloatTensor.