How to Convert a Numpy Array to Tensor?


Numpy is a popular Python library used for numerical computing and scientific computing, providing a powerful array object for handling large and multi−dimensional arrays. However, when it comes to machine learning, deep learning, and neural networks, PyTorch is a widely used library that provides an efficient and flexible platform for building and training these models.

While Numpy arrays and PyTorch tensors are similar in many ways, they have different properties and methods, which makes it necessary to convert a Numpy array to a PyTorch tensor when using PyTorch for machine learning applications. In this article, we will explore the process of converting a Numpy array to a PyTorch tensor and discuss some use cases where this conversion might be necessary. We will also demonstrate how to perform this conversion with a simple code example.

Here are two approaches to convert a Numpy array to a PyTorch tensor:

Approach 1: Using torch.tensor()

  • Import the necessary libraries − PyTorch and Numpy

  • Create a Numpy array that you want to convert to a PyTorch tensor

  • Use the torch.tensor() method to convert the Numpy array to a PyTorch tensor

  • Optionally, specify the dtype parameter to ensure that the tensor has the desired data type

  • The resulting tensor will have the same shape and data type as the original Numpy array.

Consider the code shown below.

Example

# Import the necessary libraries
import torch
import numpy as np

# Create a Numpy array
numpy_array = np.array([1, 2, 3])

# Convert Numpy array to Tensor using torch.tensor()
tensor = torch.tensor(numpy_array)

# Print the original Numpy array and the resulting Tensor
print("Numpy array:", numpy_array)
print("Tensor:", tensor)

Explanation

In this example, we first import the necessary libraries − PyTorch and Numpy. Then we create a simple 1D Numpy array. We then use the torch.tensor() method to convert the Numpy array to a PyTorch tensor, and store the resulting tensor in the variable tensor. Finally, we print the original Numpy array and the resulting tensor to verify that the conversion was successful.

Output

Numpy array: [1 2 3]
Tensor: tensor([1, 2, 3])

Approach 2: Using torch.from_numpy()

  • Import the necessary libraries − PyTorch and Numpy

  • Create a Numpy array that you want to convert to a PyTorch tensor

  • Use the torch.from_numpy() method to convert the Numpy array to a PyTorch tensor

  • The resulting tensor will share the same underlying data with the original Numpy array, which can be useful for memory efficiency when working with large datasets.

  • Optionally, you can specify the dtype parameter to ensure that the tensor has the desired data type.

Consider the code shown below.

Example

 # Import the necessary libraries
import torch
import numpy as np

# Create a Numpy array
numpy_array = np.array([1, 2, 3])

# Convert Numpy array to Tensor using torch.from_numpy()
tensor = torch.from_numpy(numpy_array)

# Print the original Numpy array and the resulting Tensor
print("Numpy array:", numpy_array)
print("Tensor:", tensor)

Explanation

In this example, we first import the necessary libraries − PyTorch and Numpy. Then we create a simple 1D Numpy array. We then use the torch.from_numpy() method to convert the Numpy array to a PyTorch tensor, and store the resulting tensor in the variable tensor. Finally, we print the original Numpy array and the resulting tensor to verify that the conversion was successful.

Note:One important thing to note about this approach is that the resulting tensor will share the same underlying data with the original Numpy array. This means that any changes made to the tensor will also affect the original Numpy array, and vice versa. This can be useful for memory efficiency when working with large datasets, but it also means that you need to be careful when modifying the data in either the Numpy array or the PyTorch tensor.

Output

Numpy array: [1 2 3]
Tensor: tensor([1, 2, 3])

Conclusion

In conclusion, converting a Numpy array to a PyTorch tensor is a simple and essential step in many machine learning and deep learning projects. In this article, we have discussed two approaches to accomplish this task − using torch.tensor() and torch.from_numpy(). Both approaches are straightforward and efficient, and the choice between them may depend on the specific requirements of the project.

Updated on: 03-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements