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.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
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
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)
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
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
Convert array to tensor using torch.Tensor()
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
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
FAQ
How to convert [[1.], [1., -1.]] to torch.tensor()?
# 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.]])
#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.
Comments
Post a Comment