import torch # create tensor x = torch.tensor([1, 2, 3]) # convert tensor to float x = x.astype(torch.float) print(x) # output: tensor([1., 2., 3.])
import torch # create tensor x = torch.tensor([1.2, 2.4, 3.6]) # convert tensor to int x = x.astype(torch.int) print(x) # output: tensor([1, 2, 3])Here, the astype() function is used to convert the data type of the Tensor object (x) from float to integer. Package library: torch Tensor module is a part of the PyTorch library.