How can I change the datatype of a tensor without changing the device type.
If I use .type() then it would also require the device (cpu or gpu). Is there any way to change just datatype of the tensor
You could just use .to()
, .type()
or call the type method directly without specifying the device
:
x = torch.randn(1, device='cuda')
print(x.to(torch.double))
> tensor([1.4169], device='cuda:0', dtype=torch.float64)
print(x.double())
> tensor([1.4169], device='cuda:0', dtype=torch.float64)
print(x.type(dtype=torch.double))
tensor([1.4169], device='cuda:0', dtype=torch.float64)
As you can see, the device is still the same.