How to Convert Pytorch tensor to Numpy array?
Last Updated :
30 Jun, 2021
In this article, we are going to convert Pytorch tensor to NumPy array.
Method 1: Using numpy().
Syntax: tensor_name.numpy()
Example 1: Converting one-dimensional a tensor to NumPy array
Python3
# importing torch module
import torch
# import numpy module
import numpy
# create one dimensional tensor with
# float type elements
b = torch.tensor([10.12, 20.56, 30.00, 40.3, 50.4])
print(b)
# convert this into numpy array using
# numpy() method
b = b.numpy()
# display
b
Output:
tensor([10.1200, 20.5600, 30.0000, 40.3000, 50.4000])
array([10.12, 20.56, 30. , 40.3 , 50.4 ], dtype=float32)
Example 2: Converting two-dimensional tensors to NumPy array
Python3
# importing torch module
import torch
# import numpy module
import numpy
# create two dimensional tensor with
# integer type elements
b = torch.tensor([[1, 2, 3, 4, 5], [3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
print(b)
# convert this into numpy array using
# numpy() method
b = b.numpy()
# display
b
Output:
tensor([[1, 2, 3, 4, 5],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
array([[1, 2, 3, 4, 5],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
Method 2: Using numpy.array() method.
This is also used to convert a tensor into NumPy array.
Syntax: numpy.array(tensor_name)
Example: Converting two-dimensional tensor to NumPy array
Python3
# importing torch module
import torch
# import numpy module
import numpy
# create two dimensional tensor with
# integer type elements
b = torch.tensor([[1, 2, 3, 4, 5], [3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
print(b)
# convert this into numpy array using
# numpy.array() method
b = numpy.array(b)
# display
b
Output:
tensor([[1, 2, 3, 4, 5],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
array([[1, 2, 3, 4, 5],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
Similar Reads
How To Convert Numpy Array To Tensor We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho
2 min read
How To Convert Numpy Array To Tensor We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho
2 min read
PyTorch Tensor vs NumPy Array PyTorch and NumPy can help you create and manipulate multidimensional arrays. This article covers a detailed explanation of how the tensors differ from the NumPy arrays. What is a PyTorch Tensor?PyTorch tensors are the data structures that allow us to handle multi-dimensional arrays and perform math
8 min read
PyTorch Tensor vs NumPy Array PyTorch and NumPy can help you create and manipulate multidimensional arrays. This article covers a detailed explanation of how the tensors differ from the NumPy arrays. What is a PyTorch Tensor?PyTorch tensors are the data structures that allow us to handle multi-dimensional arrays and perform math
8 min read
Convert PyTorch Tensor to Python List PyTorch, a widely-used open-source machine learning library, is known for its flexibility and ease of use in building deep learning models. A fundamental component of PyTorch is the tensor, a multi-dimensional array that serves as the primary data structure for model training and inference. However,
3 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read