In this Python tutorial, we will learn about PyTorch Numpy to Tensor. The PyTorch Numpy to Tensor is defined as a process that creates a Tensor from a NumPy.ndarray. And additionally, we will also cover the different examples related to the PyTorch NumPy to tensor. And also covers these topics.
- PyTorch numpy to tensor
- PyTorch numpy to tensor float64
- Pytorch numpy to tensor GPU
- Pytorch numpy to tensor dtype
- PyTorch numpy.ndarray to tensor
- PyTorch transforms numpy to tensor
- PyTorch numpy to tensor int
- Pytorch numpy to tensor CPU
- PyTorch numpy to CUDA tensor
- PyTorch numpy to tensor list
- PyTorch numpy image to tensor
PyTorch numpy to tensor
In this section, we will learn about the PyTorch NumPy to tensor in python.
Tensor is a specified data structure that is very close to array and matrices. In Pytorch we use a tensor to encrypt the inputs and outputs of a model.
The torch.from_numpy() function gives support for the transformation of a numpy array into a tensor.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- data = [[2, 4], [4, 8]] is used as a data.
- n_data = torch.tensor(data) is used as tensor can created directly from data.
- print(n_data) is used to print the n_data by using print() function.
- t_num = torch.from_numpy(num_array) Here Tensor can be created from numpy array.
- print(t_num) is used to print the t_num by using print() function.
# Importing libraries
import torch
import numpy as np
# Tensor can created directly from numdata
num_data = [[2, 4], [4, 8]]
n_data = torch.tensor(num_data)
print(n_data)
# Tensor can be created from numpy array
num_array = np.array(num_data)
t_num = torch.from_numpy(num_array)
print(t_num)
Output:
After running the above code, we get the following output in which we can see that the numpy array is converted into a tensor and the result is printed on the screen.
So, with this, we understood how to convert NumPy to tensor.
Read: PyTorch RNN – Detailed Guide
PyTorch numpy to tensor float64
In this section, we will learn about how to convert PyTorch numpy to tensor float 64 in python.
Before moving forward we should have a piece of knowledge about the float. Float is a datatype that includes the fractions represent in the decimal format.
PyTorch numpy to tensor float64 is used to convert the numpy array to tensor float64 array.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- data = [[2., 4], [4, 8], [5,10]] is used as a data.
n_data = torch.tensor(data) is used as tensor can created directly from data. - print(n_data) is used to print the n_data by using print() function.
t_num = torch.from_numpy(num_array) Here Tensor can be created from numpy array.
print(t_num) is used to print the t_num by using print() function. - print(t_num) is used to print the tensor array.
- print(t_num.shape) is used to print the tensor array shape.
- print(t_num.dtype) is used to print the tensor array datatype.
# Importing libraries
# Importing libraries
import torch
import numpy as np
# Tensor can created directly from numpy data
data = [[2., 4], [4, 8], [5,10]]
n_data = torch.tensor(data)
print(n_data)
# Tensor can be created from numpy array
num_array = np.array(data)
t_num = torch.from_numpy(num_array)
# Print output
print(t_num)
print(t_num.shape)
print(t_num.dtype)
Output:
In the following output, you can see that the PyTorch numpy array is converted into tensor float64.
This is how we can convert the numpy array into a tensor float by using torch.from_numpy() function.
Read: PyTorch MNIST Tutorial
Pytorch numpy to tensor GPU
In this section, we will learn about how to convert the PyTorch numpy to tensor GPU in python.
The PyTorch tensor is the same as a numpy ndarrays, except the tensor can run on the GPU. It is just an n-dimensional array and used arbitrary numerical computation.
PyTorch numpy to tensor GPU is a process of converting numpy to tensor GPU. The tensor is stored on the GPU and shared the same memory.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- x = np.eye(4) is used as an in-built function that is used in order to return the matrix.
- torch.from_numpy(x).to(“cuda”) is used to convert numpy to tensor GPU.
# Importing linraries
import torch
import numpy as np
# Using np.eye() function
x = np.eye(4)
# Converting numpy to tensor GPU
torch.from_numpy(x).to("cuda")
Output:
After running the above code, we get the following output in which we can see that the conversion of numpy to tensor GPU is printed on the screen.
So, with this, we understood how to convert numpy to tensor GPU.
Read: PyTorch fully connected layer
Pytorch numpy to tensor dtype
In this section, we will learn about how to convert The PyTorch numpy to tensor dtype in python.
Before moving forward we should have a piece of knowledge about dtype.
The dtype is a datatype that describes how many bytes a fixed size of the block of the memory keeps in touch with an array. Types of data types are integer, float, etc.
PyTorch numpy to tensor dtype is defined as a process to convert numpy to tensor dtype array.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- array = numpy.array([2.0, -0.4, 4.4, -3.1, 0.1]) is used as an numpy array of size five.
- print(array) is used to print the array by using the print() function.
- tensor = torch.from_numpy(array) is used to apply the from_numpy() function and storing the resulting tensor in ‘tensor’.
- print(tensor) is used to print the tensor by using the print() function.
# Importing the PyTorch library
import torch
import numpy
# A numpy array of size 5
array = numpy.array([2.0, -0.4, 4.4, -3.1, 0.1])
print(array)
# Applying the from_numpy function and
# storing the resulting tensor in 'tensor'
tensor = torch.from_numpy(array)
print(tensor)
Output:
After running the above code, we get the following output in which we can see that the PyTorch numpy to tensor dtype is printed on the screen.
This is how we can convert the numpy array into a tensor dtype and also get the datatype of the tensor array.
Read: PyTorch Binary Cross Entropy
PyTorch numpy.ndarray to tensor
In this section, we will learn about how to convert the PyTorch numpy.ndarray to tensor in python.
The PyTorch tensor is the same as a NumPy ndarrays, except the tensor can run on the GPU. It is just an n-dimensional array and used arbitrary numerical computation.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- b = np.array([[2,4,6],[3,1,2],[3,4,5],[6,7,5]]) is used create a numpy.ndarray b.
- print(“b:\n”, b) is used to print the value b by using print() function.
- print(“Type of b :\n”, type(b)) is used to print the type of b.
- ten = torch.from_numpy(b) is used to convert the numpy.ndarray to tensor.
- print(“tensor:\n”, ten) is used to print the tensor by using print() function.
- print(“Type after conversion:\n”, type(ten)) is used to print the type after conversion.
# import the libraries
import torch
import numpy as np
# Create a numpy.ndarray "b"
b = np.array([[2,4,6],[3,1,2],[3,4,5],[6,7,5]])
print("b:\n", b)
print("Type of b :\n", type(b))
# Convert the numpy.ndarray to tensor
ten = torch.from_numpy(b)
print("tensor:\n", ten)
print("Type after conversion:\n", type(ten))
Output:
In the following output, you can see that the PyTorch numpy.ndarray to tensor value is printed on the screen.
So, with this, we understood how to convert numpy.ndarray to tensor.
Read: PyTorch Logistic Regression
PyTorch transforms numpy to tensor
In this section, we will learn about how to transform the PyTorch numpy to tensor in python.
The PyTorch tensor is similar to a numpy.ndarray. Here we will transform the numpy array into a tensor with the help of the torch.from_numpy() function.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- array = np.array([2, 4, 6, 8, 10, 12]) is used to describe the variable by using np.array() function.
- tensor_x = torch.from_numpy(array) is used to convert numpy array to tensor.
- print(tensor_x) is used to print the tensor x values.
- print(tensor_y) is used to print the tensor y values.
- print(tensor_z) is used to print the tensor y values.
# Importing Libraries
import torch
import numpy as np
# Describing an array
array = np.array([2, 4, 6, 8, 10, 12])
# Convert Numpy array to torch.Tensor
tensor_x = torch.from_numpy(array)
tensor_y = torch.Tensor(array)
tensor_z = torch.tensor(array)
# Print outputs
print(tensor_x)
print(tensor_y)
print(tensor_z)
Output:
After running the above code, we get the following output in which we can see that the PyTorch transforms numpy to tensor array is printed on the screen.
This is how we can convert the numpy array into a tensor dtype and also get the datatype of the tensor array.
Read: PyTorch Early Stopping + Examples
PyTorch numpy to tensor int
In this section, we will learn how to convert the PyTorch numpy to tensor int in python.
The PyTorch numpy to tensor is a process in which we are converting the numpy array to tensor int with the help of torch.from_numpy() function.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- n_data = np.array([[2, 4], [6, 8], [10,12]]) is defined as an numpy array.
- numdata = torch.tensor(n_data) Here the tensor can created directly from n_data by using torch.tensor().
- t_num = torch.from_numpy(num_array) is used to convert numpy to tensor with the help of torch.from_numpy() function.
- print(t_num) is used to print the t_num by using print() function.
- print(t_num.shape) is used to print the shape of the t_num.
- print(t_num.dtype) is used to print the datatype of the t_num.
# Importing libraries
import torch
import numpy as np
# Tensor can created directly from n_data
n_data = [[2, 4], [6, 8], [10,12]]
numdata = torch.tensor(n_data)
print(numdata)
# Tensor can be created from numpy array
num_array = np.array(n_data)
t_num = torch.from_numpy(num_array)
# Print output
print(t_num)
print(t_num.shape)
print(t_num.dtype)
Output:
In the below output, you can see that the conversion of the PyTorch numpy to tensor int is printed on the screen.
So with this, we understood how to convert the PyTorch numpy to tensor int.
Read: PyTorch MSELoss – Detailed Guide
PyTorch numpy to tensor CPU
In this section, we will learn about how to convert the PyTorch numpy to tensor CPU in python.
The PyTorch numpy to tensor CPU is a process of converting numpy to tensor CPU. The NumPy array is stored on the CPU and shares the same memory.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- numpy_array = np.array([[3,6,9],[4,8,16],[5,10,25]]) is used to creating a numpy array by using np.array() function.
- print(numpy_array) is used to print the numpy array with the help of print() function.
- tensor_arr = torch.from_numpy(numpy_array) is used to convert the numpy array to tensor CPU with the help of torch.from_tensor() function.
# Importing libraries
import torch
import numpy
# Create a numpy array
numpy_array = np.array([[3,6,9],[4,8,16],[5,10,25]])
print(numpy_array)
# Comversion of numpy to tensor CPU
tensor_arr = torch.from_numpy(numpy_array)
tensor_arr
Output:
After running the above code, we get the following output in which we can see that the conversion of the PyTorch numpy to tensor CPU is printed on the screen.
This is how we can convert the PyTorch numpy array into a tensor CPU.
Read: PyTorch Tensor to Numpy
PyTorch numpy to CUDA tensor
In this section, we will learn about how to convert the PyTorch numpy to CUDA tensor in python.
Before moving forward we should have some piece of knowledge about CUDA.
CUDA stands for compute unified device architecture which is an application programming interface that permits the software to use certain types of GPU.
The PyTorch numpy to CUDA tensor is defined as a process that converts PyTorch numpy to CUDA tensor.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- numpy_array = np.array([[3,6,9],[4,8,16],[5,10,25]]) is used to create a numpy array.
- print(numpy_array) is used to print the numpy array by using print() function.
- device = torch.device(“cuda:0” if torch.cuda.is_available() else “CPU”) Here we are declaring the device and check CUDA is available or not.
- tensor_gpu=torch.from_numpy(numpy_array).to(device) is used to convert the numpy to CUDA tensor.
- print(tensor_gpu) is used to print the CUDA tensor.
# Importing libraries
import torch
import numpy
# Create a numpy array
numpy_array = np.array([[3,6,9],[4,8,16],[5,10,25]])
print(numpy_array)
# Declaring the device
# check Cuda is available or not
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Converting numpy to CUDA tensor
tensor_gpu=torch.from_numpy(numpy_array).to(device)
print(tensor_gpu)
Output:
In the below output, you can see that the conversion of the PyTorch numpy to CUDA tensor is printing on the screen.
So with this, we understood how to convert the PyTorch numpy to CUDA tensor.
Read: Keras Vs PyTorch
PyTorch numpy to tensor list
In this section, we will learn about how to convert the PyTorch numpy to tensor list in python.
The PyTorch numpy to tensor list is defined as a process that converts PyTorch numpy to tensor list with the help of tensor.from_numpy() function.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import numpy as np.
- array = np.array([[3,9,15],[4,12,20],[5,15,30]]) is used to create a numpy array.
- print(array) is used to print the numpy array using print() function.
- list_of_tensor = torch.from_numpy(numpy_array) is used to convert the numpy array to tensor list using torch.from_numpy() function.
- print(list_of_tensor) is used to print the list of the tensor.
# Importing libraries
import torch
import numpy
# Create a numpy array
array = np.array([[3,9,15],[4,12,20],[5,15,30]])
print(array)
# Comversion of numpy to tensor CPU
list_of_tensor = torch.from_numpy(numpy_array)
print(list_of_tensor)
Output:
After running the above code, we get the following output in which we can see that the conversion of the PyTorch numpy to tensor list is printed on the screen.
This is how we can convert the PyTorch numpy to a tensor list.
Read: PyTorch Load Model + Examples
PyTorch numpy image to tensor
In this section, we will learn about how to convert PyTorch numpy image to tensor in python.
The PyTorch numpy image to tensor is defined as a process that converts PyTorch numpy image to tensor by using tensor.from_numpy() function.
Code:
In the following code, firstly we will import all the necessary libraries such as import torch, and import io from skimage.
- img = io.imread(‘input.png’) is used as reading an image.
- device = torch.device(“cuda:0” if torch.cuda.is_available() else “CPU”) is used as a device used in this conversion.
- img = torch.from_numpy(img).float() is used to convert the PyTorch numpy image to tensor.
- print(img.device) is used to print the image.device with the help of print() function.
# Importing libraries
import torch
from skimage import io
# Reading an image
img = io.imread('input.png')
# Using the devide
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
# Converting numpy image to tensor
img = torch.from_numpy(img).float()
print(img.device)
Output:
In the below output, you can see that the conversion of numpy image to tensor is printed on the screen.
You may also like to read the following PyTorch tutorials.
- PyTorch nn linear + Examples
- Cross Entropy Loss PyTorch
- PyTorch Activation Function
- Adam optimizer PyTorch
- PyTorch Reshape Tensor
- How to Convert NumPy Tuple to List in Python
So, in this tutorial, we discussed PyTorch numpy to tensor and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- PyTorch numpy to tensor
- PyTorch numpy to tensor float64
- Pytorch numpy to tensor GPU
- Pytorch numpy to tensor dtype
- PyTorch numpy.ndarray to tensor
- PyTorch transforms numpy to tensor
- PyTorch numpy to tensor int
- PyTorch numpy to tensor CPU
- PyTorch numpy to CUDA tensor
- PyTorch numpy to tensor list
- PyTorch numpy image to tensor
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.