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 two approaches to convert a NumPy array to a PyTorch tensor and discuss the key differences between them.

Using torch.tensor()

The torch.tensor() method creates a new tensor by copying data from the NumPy array. This approach creates an independent tensor that doesn't share memory with the original array.

Example

import torch
import numpy as np

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

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

# Print both arrays
print("NumPy array:", numpy_array)
print("Tensor:", tensor)
print("Tensor dtype:", tensor.dtype)
NumPy array: [1 2 3 4 5]
Tensor: tensor([1, 2, 3, 4, 5])
Tensor dtype: torch.int64

Using torch.from_numpy()

The torch.from_numpy() method creates a tensor that shares the same underlying data with the original NumPy array. This approach is more memory-efficient but means changes to one will affect the other.

Example

import torch
import numpy as np

# Create a NumPy array
numpy_array = np.array([1.0, 2.0, 3.0, 4.0, 5.0])

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

print("Original NumPy array:", numpy_array)
print("Original tensor:", tensor)

# Modify the NumPy array
numpy_array[0] = 100

print("After modifying NumPy array:")
print("NumPy array:", numpy_array)
print("Tensor:", tensor)  # Notice the tensor also changed
Original NumPy array: [1. 2. 3. 4. 5.]
Original tensor: tensor([1., 2., 3., 4., 5.], dtype=torch.float64)
After modifying NumPy array:
NumPy array: [100.   2.   3.   4.   5.]
Tensor: tensor([100.,   2.,   3.,   4.,   5.], dtype=torch.float64)

Working with Multi-dimensional Arrays

Both methods work seamlessly with multi-dimensional NumPy arrays ?

import torch
import numpy as np

# Create a 2D NumPy array
numpy_2d = np.array([[1, 2, 3], [4, 5, 6]])

# Convert using both methods
tensor1 = torch.tensor(numpy_2d)
tensor2 = torch.from_numpy(numpy_2d)

print("2D NumPy array shape:", numpy_2d.shape)
print("Tensor1 shape:", tensor1.shape)
print("Tensor2 shape:", tensor2.shape)
print("\nTensor1:\n", tensor1)
2D NumPy array shape: (2, 3)
Tensor1 shape: torch.Size([2, 3])
Tensor2 shape: torch.Size([2, 3])

Tensor1:
 tensor([[1, 2, 3],
        [4, 5, 6]])

Comparison

Method Memory Sharing Performance Best For
torch.tensor() No (creates copy) Slower for large arrays Independent operations
torch.from_numpy() Yes (shares memory) Faster, memory-efficient Memory-constrained scenarios

Conclusion

Converting NumPy arrays to PyTorch tensors is essential for machine learning workflows. Use torch.tensor() when you need independent data copies, and torch.from_numpy() for memory-efficient operations where data sharing is acceptable.

Updated on: 2026-03-27T11:09:41+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements