una_dinosauria
una_dinosauria

Reputation: 2071

Torch - How to change tensor type?

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?

Upvotes: 70

Views: 251009

Answers (3)

Rev_Nge
Rev_Nge

Reputation: 471

use .to method of torch as follows:

y = y.to(torch.long)

More details about torch tensor type/ops can be found here

https://pytorch.org/docs/stable/tensors.html

Upvotes: 47

Dharma
Dharma

Reputation: 2663

For pytorch users, because searching for change tensor type in pytorch in google brings to this page, you can do:

y = y.type(torch.LongTensor)

Upvotes: 23

una_dinosauria
una_dinosauria

Reputation: 2071

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.

Upvotes: 95

Related Questions