Pytorch Basics Tutorial featured image

PyTorch Basics Tutorial: A Complete Overview With Examples

PyTorch is an Open Source Python library that has been developed for the replacement of numpy library and for fast deep learning research. Most of the beginners know only about machine learning libraries like Numpy for mathematical calculation and Tensorflow for deep learning. But in this entire tutorial, you will know the Pytorch basics that are backed by the Social Giant Facebook. You will know the following things.

Empty Tensors
Creating Tensors from the Data
Check the Size of the Tensor
Operations on the Tensor
Conversion of tensor to Numpy

Deep Learning Model do most of the computation on tensors. Tensors are like Numpy ndarrays. The one difference between these two libraries is that Numpy does computation on CPU but the tensors can do computation on GPU also. Let’s understand all the Pytorch Basics.

Empty Tensors

You can create empty tensors using empty() method with the size of the matrix. Like in this case 5,4 (5 rows and 4 columns).

x = torch.empty(5,4)

empty tensors

You can also define the type of the values using the dtype. Like I want to create tensor of one’s values of type integer then I will use dtype =

x= torch.ones(5,4,

ones tensors with type integer

Otherwise, the default is long

Creating Tensors from the Data

For creating tensors from the exiting data you have to just pass the data into the tensor() method.

# tensors from the data
data = [[1,2,3,],
y = torch.tensor(data)

tensors from the data

Check the Size of the Tensor

You can check the size of the tensor using the method size() method.


size of the tensor

Operations on the Tensor

Just like you do operations on numpy you can do operations on the tenors also. Like I want to add some numbers to the tensor then I will use the following code.

x = torch.tensor([[2,2,2,2],
y = torch.tensor([[3,3,3,3],
sum = torch.add(x,y)

operations on the tensor


Traversing in the Tensors is the same as Numpy. For example I want to access the value at the first location of added value then I will use the below code.


traversing on the tensor


You can convert the tensors to numpy and numpy to tensors using Pytorch module

Torch Tensor to Numpy Array

p = torch.ones(5)
q = p.numpy()

The numpy() method uses to convert the tenors to the numpy array.

Numpy to Torch Tensor

You have to use from_numpy() method for converting the tensor to numpy array.

r = np.ones(5)
s = torch.from_numpy(r)

These are the basics of the PyTorch that are necessary to know before moving on to build a Deep Learning Model using PyTorch. Creating empty tensors, from the data, traversing the tensors and conversion are generally used while the creation of the deep learning model.

We hope that you have liked this entire tutorial. Keep visiting our site for more updates. if you have any queries on Pytorch then Please contact us. We are always ready to help you.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner