Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Torch - How to change tensor type?

Tags:

torch

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?

like image 785
una_dinosauria Avatar asked Dec 10 '15 02:12

una_dinosauria


People also ask

How do I get Dtype of a torch tensor?

A PyTorch tensor is homogenous, i.e., all the elements of a tensor are of the same data type. We can access the data type of a tensor using the ". dtype" attribute of the tensor. It returns the data type of the tensor.

How do you convert a float tensor to a double tensor PyTorch?

To convert this FloatTensor to a double, define the variable double_x = x. double(). We can check the type and we see that whereas before this PyTorch tensor was a FloatTensor, we now have a PyTorch tensor that is a DoubleTensor.

What type is torch tensor?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.


2 Answers

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.

like image 85
una_dinosauria Avatar answered Oct 04 '22 00:10

una_dinosauria


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

like image 45
Rev_Nge Avatar answered Oct 03 '22 23:10

Rev_Nge