NumPy is the most popular multidimensional array library, it has now arguably become the lingua franca of data science. PyTorch tensor seamless interoperability with NumPy.

NumPy is ubiquity in the Python data science ecosystem, which brings with it first-class integration with the rest of the scientific libraries in Python, such as SciPy, Scikit-learn, and Pandas.

NumPy has excellent interoperability with PyTorch. PyTorch tensors can be converted to NumPy arrays and vice versa very efficiently. By doing so, we can take advantage of the huge swath of functionality in the wider Python ecosystem that has built up around the NumPy array type.

To get a NumPy array out of our tensor, we just call tensor.numpy() , which will return a NumPy multidimensional array of the right size, shape, and numerical type.

import torch

x_ten=torch.tensor([[23.13,32.94,33.32],
                [42.32,59.67,62.32]],dtype=torch.float32)

x_np=x_ten.numpy()

print(type(x_np)) #<class 'numpy.ndarray'>
print(x_np)

The returned array shares the same underlying buffer with the tensor storage. This means the numpy method can be effectively executed at no cost, as long as the data sits in CPU RAM. 

It also means modifying the NumPy array will lead to a change in the originating tensor. If the tensor is allocated on the GPU, PyTorch will make a copy of the content of the tensor into a NumPy array allocated on the CPU. 

Conversely, we can obtain a PyTorch tensor from a NumPy array this way which will use the same buffer-sharing strategy. 

x_ten = torch.from_numpy(x_np)

While the default numeric type in PyTorch is 32-bit floating-point, for NumPy it is 64-bit. We usually want to use 32-bit floating-points, so we need to make sure we have tensors of dtype torch .float after converting.

Related Post

How to Create PyTorch Tensor From List and NumPy Array?

What is tensor in PyTorch?

Fix: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Create NumPy array from PyTorch Tensor using detach().numpy()

How to change the PyTorch tensor type?

PyTorch Contiguous Tensor

How to create an empty tensor in PyTorch?

Difference Between Contiguous and Non-Contiguous Array