Torch - How to change tensor type?

una_dinosauria picture una_dinosauria · Dec 10, 2015 · Viewed 25.6k times · Source

I created a permutation of the numbers from 1 to 3.

th> y = torch.randperm(3 );
th> y
 3
 2
 1
[torch.DoubleTensor of size 3]

Now, I want to convert y to a Torch.LongTensor. How can I do that?

Answer

una_dinosauria picture una_dinosauria · Dec 10, 2015

y = y.long() does the job. There are similar methods for other data types, such as int, char, float and byte.

You can check different dtypes here.