python-pytorchHow can I convert a Python PyTorch tensor to a NumPy array?
To convert a Python PyTorch tensor to a NumPy array, you can use the PyTorch .numpy()
method. This method will return the tensor as a NumPy array.
Example
import torch
x = torch.tensor([1, 2, 3])
y = x.numpy()
print(y)
Output example
[1 2 3]
The code above consists of the following parts:
import torch
imports the PyTorch library.x = torch.tensor([1, 2, 3])
creates a PyTorch tensor with values 1, 2, and 3.y = x.numpy()
converts the PyTorch tensor to a NumPy array.print(y)
prints the NumPy array.
Helpful links
More of Python Pytorch
- How can I use PyTorch with Python 3.10?
- How do I install a Python PyTorch .whl file?
- How do I install PyTorch on a Windows computer?
- How can I use Python PyTorch without CUDA?
- How do I check the version of Python and PyTorch I am using?
- How can I compare the performance of PyTorch Python and C++ for software development?
- How can I use Python and PyTorch together with Xorg?
- How do I uninstall Python PyTorch?
- How do Python and PyTorch compare for software development?
- How do I download Python and Pytorch?
See more codes...