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**

**Traversing**

**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)
print(x)
```

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 = torch.int.

```
x= torch.ones(5,4,dtype=torch.int)
print(x)
```

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,],
[4,5,6]]
y = torch.tensor(data)
print(y)
```

## Check the Size of the Tensor

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

`print(y.size())`

## 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],
[3,3,3,3]])
y = torch.tensor([[3,3,3,3],
[4,4,4,4]])
sum = torch.add(x,y)
print(sum)
```

## Traversing

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.

`print(sum[:,1])`

## Conversion

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()
print(q)
print(type(q))
```

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)
print(s)
print(type(s))
```

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.