Pytorch Introduction

Pytorch is an open-source Python library that is used for building and training deep learning models by Facebook. It is released under the Modified BSD license.


History

The Pytorch project was started by Adam Paszke under the guidance of Soumith Chintala in October of 2016. It inherits its features from 2 libraries. Firstly, Torch, which is a deep learning library written in Lua that appeared in 2002. Soumith Chintala was also a core developer in the Torch project. The Torch library, however, is no longer in active development as of now. Secondly, Chainer, a python library for deep learning built by a Japanese company on top of Numpy and CuPy.


Why learn Pytorch?

Pytorch and Tensorflow are undoubtedly the most used libraries used for training deep learning models. The question that pops up next is why to learn Pytorch instead of any other library used for training deep learning models, mostly Tensorflow. In short, what advantages does Pytorch provide over Tensorflow? This answer is that both have their own advantages and disadvantages. For example, while Pytorch is easier to learn and implement as compared to Tensorflow, it does not have a community nearly as big as Tensorflow. Moreover, Pytorch has been quite famous within the Deep Learning research community lately.

The table below draws a comparison between Pytorch and Tensorflow.

PytorchTensorflow
Uses dynamic graphs.Version 1: Uses static graphs.
Version 2: Dynamic graphs can be used.
Has a less steep learning curve.Has a much steeper learning curve.
Is open-source.Is open-source
Developed by Facebook.Developed by Google.
Doesn’t have its own tool for visualization.Comes with Tensorboard: Tensorflow’s ‘magic’ tool for visualization.
Has a comparatively smaller community.Has a very large community.

Do you have Pytorch?

You can check if you already have Pytorch installed by running the following command in your Python interpreter or Jupyter Notebook.

import torch

If you don’t have pytorch installed, an error message similar to the one below will be displayed.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'

However, if no such error message appears, then pytorch is already installed and you are good to continue with the rest of this pytorch introduction tutorial.


Installing Pytorch

Pytorch is easy to install. The procedure for installing it depends on your package manager.

Installing Pytorch using Pip

Pip is the default package management system for python. Therefore, it comes bundled with your python interpreter and is not required to be installed separately. To install pytorch using pip, simply run the following command

pip install torch

Installing Pytorch using Conda

Conda is the default package manager of the Anaconda distribution, which is just a bunch of data science packages bundled together. As a result, it comes bundled with the anaconda distribution. If you use the Anaconda distribution, you should be using conda to manage your packages and hence to install Pytorch. To install pytorch using conda, simply run the following command.

conda install -c pytorch pytorch

Checking Pytorch Version

You can check the version of Pytorch you are using by running the following command

print(torch.__version__)

This Pytorch Introduction tutorial is written using the Pytorch 1.6.0 version, therefore it is recommended to use any of the 1.6.x version or at least a 1.x version.

Note– It is also recommended that you use Python 3 instead of Python 2 for this tutorial.

With Pytorch installed on your system, you are good to follow along with this pytorch introduction tutorial.