PyTorch - How to convert array to tensor?

In PyTorch, we use torch.from_numpy() method to convert an array to tensor. This method accepts numpy.ndarray and converts it to a torch tensor of the same dtype as of array. It supports numpy.ndarray of the dtypes -float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool. 

Other convenient methods, we can use, are - torch.tensor() and torch.Tensor(). These methods can also be used to convert the array to torch tensor. For Deep Learning task, we advise to use torch.Tensor() as PyTorch uses float32 as default dtype of input data and this method convert array of any dtype to float32. The torch.tensor() converts the array to tensor of same dtype as of array.

Let's discuss the methods in detail.

Syntax

We can use the following syntax/es to convert an array to torch tensor.
torch.from_numpy(arr)
torch.tensor(arr)
torch.Tensor(arr)

Here arr is a NumPy ndarray to be converted to torch tensor. The first syntax is most convenient to convert NumPy Array to a PyTorch tensor. The other syntaxes are also be used to convert array to tensor.

Convert array to tensor using torch.from_numpy() method

The torch.from_numpy() is used to convert an array (NumPy ndarray) to torch tensor. It accepts numpy.ndarray and returns a tensor of same dtype as of ndarray.
Look art the below example:

Example 1

In the following Python program, we convert a numpy ndarray to torch tensor. We print the type before and after the conversion. We also print the dtype of array and tensor.
# Python program to convert array to tensor in PyTorch using torch.from_numpy() method
import torch
import numpy as np
arr = np.array([1.,2.,3.,4.,5.])
print(type(arr)) print(arr.dtype)
tens = torch.from_numpy(arr)
print(type(tens))
print(tens)
print(tens.dtype)

Output

<class 'numpy.ndarray'>
float64
<class 'torch.Tensor'>
tensor([1., 2., 3., 4., 5.], dtype=torch.float64)
torch.float64

Notice NumPy uses default floating point as float64. After the conversion the torch tensor is also of float64. 

When we deal with Deep Learning, we should make sure that the input tensor is in the same dtype as the layer parameters. If the tensor input to the layer is created using numpy arrays this may raise an error because PyTorch uses  float32 as default type whereas the numpy uses float 64.

So to deal with this problem, we can create original tensor as of float32 type and then convert the numpy array to torch tensor.

Example 2

In the following Python program, we create a numpy.ndarray of float32 dtype and then convert it into a torch tensor using torch.from_numpy() method. We also print type and dtype before and after conversion.
# Python program to convert array to tensor in PyTorch using torch.from_numpy() method
import torch
import numpy as np
arr = np.array([1.,2.,3.,4.,5.], dtype=np.float32)
print(type(arr)) print(arr.dtype)
tens = torch.from_numpy(arr)
print(type(tens))
print(tens)
print(tens.dtype)

Output:

<class 'numpy.ndarray'>
float32
<class 'torch.Tensor'>
tensor([1., 2., 3., 4., 5.])
torch.float32

Notice the created array is of dtype float32 and the converted tensor is also of dtype float32.

Example 3 : Convert an array of lists to tensor

In the following example, we convert a numpy array of lists to torch tensor using the torch.from_numpy() method.
# Python program to convert array of lists to tensor in PyTorch using torch.from_numpy() method
import torch
import numpy as np
arr = np.array([
list([0., 0, 1, 0, 0]),
list([0, 1, 0, 0, 0]),
list([0, 0, 0, 1, 0])
])


print(type(arr))0l;-;p=[]\
tens = torch.from_numpy(arr)
print(type(tens))
print(tens)

Output:

<class 'numpy.ndarray'>
<class 'torch.Tensor'>
tensor([[0., 0., 1., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 0., 1., 0.]], dtype=torch.float64)

Notice we created an array of lists and the array is converted to a tensor. Also notice the array has only a single entry as floating point.

Convert array to tensor using torch.tensor()

We could also use the torch.tensor() to convert a numpy array into torch tensor. It also convert array to tensor of same type as of array. To convert array to a tensor of specific dtype, we  pass a second parameter to it (see Example 2 below). Let's look at the example programs:

Example 1

In the below example program,we convert an array to torch tensor and print the type of array and tensor. You will notice array is a numpy.ndarray and tensor is torch.Tensor.
import torch
import numpy as np
arr = np.array([1.,2.,3.,4.,5.])
print(type(arr))
tens = torch.tensor(arr)
print(type(tens))
print(tens)
print(tens.dtype)

Output:

<class 'numpy.ndarray'>
<class 'torch.Tensor'>
tensor([1., 2., 3., 4., 5.], dtype=torch.float64)
torch.float64

Notice the output tensor is of dtype float64. To convert array to a tensor of specific dtype, we  
pass a second parameter to it. Let's look at an example below:

Example 2

In this example we convert a numpy.ndarray to a tensor of int32 dtype.
import torch
import numpy as np
arr = np.array([1.,2.,3.,4.,5.])
print(type(arr))
tens = torch.tensor(arr,dtype = torch.int32)
print(type(tens))
print(tens)
print(tens.dtype)

Output

<class 'numpy.ndarray'>
<class 'torch.Tensor'>
tensor([1, 2, 3, 4, 5], dtype=torch.int32)
torch.int32

Notice the output tensor is of dtype torch.int32.

Convert array to tensor using torch.Tensor()

We could also use the torch.Tensor() to convert a numpy array into torch tensor. It also convert array to tensor of torch.float32 dtype irrespective of dtype of array. Let's look at the example programs:

Example  1

import torch
import numpy as np
arr = np.array([1.,2.,3.,4.,5.])
print(type(arr)) print(arr.dtype)
tens = torch.Tensor(arr)
print(type(tens))
print(tens)
print(tens.dtype)

Output

<class 'numpy.ndarray'>
float64
<class 'torch.Tensor'>
tensor([1., 2., 3., 4., 5.])
torch.float32

Notice the numpy.ndaary (dtype float64) is converted to torch Tensor of dtype float32.

Example 2

In the below example a numpy array of int32 to a torch tensor of float32.
import torch
import numpy as np
arr = np.array([1,2,3,4,5])
print(type(arr)) print(arr.dtype)
tens = torch.Tensor(arr)
print(type(tens))
print(tens)
print(tens.dtype)

Output

<class 'numpy.ndarray'>
int32
<class 'torch.Tensor'>
tensor([1., 2., 3., 4., 5.])
torch.float32

Notice the array is of dtype int32 and the output tensor is of flaot32.

FAQ

How to convert [[1.], [1., -1.]] to torch.tensor()?

First let's create the array given in the question.
# How to convert [[1.], [1., -1.]] to torch.tensor()?
import torch
import numpy as np


arr = np.array([[1.], [1., -1.]])

Output

C:\Users\Public\Documents\Wondershare\CreatorTemp/ipykernel_1548/2887996107.py:5: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  arr = np.array([[1.], [1., -1.]])

So the error show that we can't create a numpy.ndarray of different length or shape. But we can create such array as an object. Let's see how.
#How to onvert [[1.], [1., -1.]] to torch.tensor() ?
import torch
import numpy as np

arr = np.array([[1.], [1., -1.]], dtype = object)
tens = torch.from_numpy(arr)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\Public\Documents\Wondershare\CreatorTemp/ipykernel_1548/3166897511.py in <module>
      4 
      5 arr = np.array([[1.], [1., -1.]], dtype = object)
----> 6 tens = torch.from_numpy(arr)

TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

The above error shows that we can't convert the numpy.ndarray of type object to a tensor.
Hence we can't convert the array given in the question to a tensor.

Conclusion

In this post, we discussed different ways to convert an array to tensor in PyTorch. The first and most convenient method is using the torch.from_numpy() method. The other method are using torch.tensor() and torch.Tensor(). The last method - torch.Tensor() converts the array to tensor of dtype = torch.float32 irrespective of the input dtype of the array whereas the torch.tensor() convert the array to tensor of same dtype as of array.

Advertisements


Comments