PyTorch is a library for Python programs that facilitates building deep learning projects. Since the library’s release, it has grown into one of the most prominent deep learning tools for a broad range of applications.
The basic building block of any Deep Learning library is a tensor. Tensors are data structures which are matrix-like very similar in function properties and function to Numpy arrays. You can think of a tensor as a Numpy array only. The thing that differentiates them is that the tensor operations can run on GPU or TPU (Tensor Processing Unit).
To use PyTorch in your application, you need to write the following line:
To declare a tensor in PyTorch you need to write the following code:
a = torch.Tensor(2, 2)
The line above will create a tensor of shape 2×2 or 2 rows and 2 columns of type float and values initialized to 0.
To create a tensor having random values:
a = torch.randn(2,2)
This line create a tensor of shape 2×2 having random values.
The type of this tensor can be found as
Operations on PyTorch tensors
We can perform a list of operations on PyTorch tensors:
Now to perform the multiplication or the dot product of two tensors we perform:
The line above finds the dot product of the two tensors and is similar to the np.dot(x,y) function of Numpy.
Now we have understood enough of tensors and now are going understand about various modules of PyTorch
This module of PyTorch contains code of most of the optimizers.
import torch adam = torch.optim.Adam(model.parameters(), lr=0.0001)
This module of PyTorch contains all functions required for neural networks
Some example of nn module are:
loss_function = nn.CrossEntropyLoss()
Creating neural network layers:
layer = nn.Linear(64, 64) layer = nn.Conv2d(1, 64, kernel_size=(3, 3), padding=1)
To save your model in PyTorch we need to use the save function of it:
Here model is the name of the model and ‘model.ckpt’ is the destination of the model file.
We will understand more about it as we will use it in our following blogs.