Created
August 24, 2023 20:55
-
-
Save Infinitusvoid/0b4e2ca92b625d3659cf08c0e35fd716 to your computer and use it in GitHub Desktop.
PyTorch : How to convert tensor to numpy array, and back ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import torch | |
# Tensor to NumPy array | |
tensor = torch.ones(7) | |
numpy_tensor = tensor.numpy() | |
tensor, numpy_tensor | |
#What about going back | |
tensor = torch.from_numpy(array) # warning: when converting from numpy -> pytorch, pytorch reflects numpy's default datatype of float64 unless specefied otherwise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment