datahacker.rs@gmail.com

#001 PyTorch – How to Install PyTorch with Anaconda?

#001 PyTorch – How to Install PyTorch with Anaconda?

Highlights: Welcome everyone! In this post we are going to cover the prerequisites for installing PyTorch on our machines. It’s relatively easy to get it up and running, so let’s roll.

Tutorial Overview:

1. PyTorch installation

Due to simplicity, the recommended option to start with is to use the Anaconda Python package manager. This is because with Anaconda it is easy to work with and manage Jupyter notebook, Python and other packages used for scientific computing and Data Science. This includes PyTorch.

Let’s install PyTorch right away.

  • Download and install Anaconda (Use the latest Python Version). You can choose the right operating system for your computer. In our case, we will download the package for Linux.
Anaconda install
  • Next, go to getting started section on the PyTorch website. Scroll below, then specify the appropriate configuration options for your particular environment. For example:
    • PyTorch Build: Stable (1.6)
    • Operation System: Linux
    • Package Manager: Conda
    • Python: 3.8
    • CUDA: 10.2
PyTorch download
  • Finally, once Anaconda is installed, open conda (e.g. from Windows start and by typing conda), run the shown command in the terminal to install PyTorch.
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch

If you look carefully at the commands notice that we are installing both PyTorch and torchvision. Also, there is no need to install CUDA separately. This is because the needed CUDA version comes installed with PyTorch since we selected the CUDA version.

Now to verify that our installation steps are complete, let’s execute the following commands below.

2. How to verify the installation?

Let’s import the top-level PyTorch package named torch.

import torch

To check for the version of PyTorch that we have installed, we can run the following command.

print(torch.__version__)
Output: 1.6.0

Since CUDA comes installed with PyTorch, when we run this cell we expect it to return True. This is because we choose the 10.2 version of CUDA during the PyTorch installation and we have an Nvidia GPU support on our system.

torch.cuda.is_available()
Output:  True

Finally, we can check the version of CUDA by running the cell below.

torch.version.cuda
Output: 10.2

Using Google Colab

Quite often Google Colab can be your default choice for Python and Deep Learning projects. If this is the case, PyTorch is already installed. So, you can just use it with

import torch

Summary

If you have made it to the end, congratulations. Now we have installed PyTorch successfully, so let’s get our hands dirty with the PyTorch basics. In the next post, we will start a brief introduction into PyTorch along with the explanations what tensors in PyTorch are.

References: