The Python Oracle

tf.cast equivalent in pytorch?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping

--

Chapters
00:00 Question
00:49 Accepted answer (Score 12)
01:10 Thank you

--

Full question
https://stackoverflow.com/questions/5913...

Accepted answer links:
[PyTorch Documentation]: https://johnwlambert.github.io/pytorch-t...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #tensorflow #pytorch

#avk47



ACCEPTED ANSWER

Score 13


Check out the PyTorch Documentation

As they mentioned:

print(x.dtype) # Prints "torch.int64", currently 64-bit integer type
x = x.type(torch.FloatTensor)
print(x.dtype) # Prints "torch.float32", now 32-bit float
print(x.float()) # Still "torch.float32"
print(x.type(torch.DoubleTensor)) # Prints "tensor([0., 1., 2., 3.], dtype=torch.float64)"
print(x.type(torch.LongTensor)) # Cast back to int-64, prints "tensor([0, 1, 2, 3])"