#001 PyTorch – How to Install PyTorch with Anaconda and use it on Colab?
Highlights: Welcome everyone! In this post we are going to cover the prerequisites for installing PyTorch on our machines. In addition, we will show how you can use PyTorch in Google Colab. It’s relatively easy to get it up and running, so let’s roll.
Tutorial Overview:
1. PyTorch installation with Anaconda
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. It will be easy to work with PyTorch with Anaconda as well, once we install it on our computer
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.
- 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
- Finally, once Anaconda is installed, open Anaconda Prompt (e.g. from Windows start and by typing Anaconda), 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. In addition, there is no need to install CUDA separately. This is because the needed CUDA version comes installed with PyTorch since we have 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
3. Using Google Colab
Quite often Google Colab can be your default choice for Python and Deep Learning projects. In addition, we are also using it to create our Python scripts. If this is the case, PyTorch is already installed. So, you can just use it with
import torch
Summary
Now that we have installed PyTorch successfully, 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.