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?
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.
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.
A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With