Installing and setting up PyTorch with Anaconda and CUDA

This tutorial explains how to setup and smoke test the deep learning framework called PyTorch, with CUDA and a Python environment manager called Anaconda.

Jun 11, 2019

pythonaianacondapytorchdeep learningmachine learningobject detectiondata science

Anaconda and Python

Anaconda is a tool for managing Python environments. You can isolate python versions from each other, as well as isolate packages from one another. This is very important when experimenting with Python and AI white papers, because the community at this time is still mixed with Python version 2.7.x and version 3.x .

Installation

We are going to assume you are on a system like Mac OSX, Linux, or Windows. This should work on all 3 of those OS's.

Download and install Anaconda : https://www.anaconda.com/distribution

When you install, we recommend installing version 3.7+ since it can support prior versions of python as well.

Your first Anaconda Environment

Anaconda is also called "Conda", and after installation you can create your first environment.. in our case we want an environment to install the latest PyTorch and it's requirements.

More on Conda Env commands : https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

> conda create --name pytorch1_py37 python=3.7

This last command creates the environment, but now you need to switch into it. Switching is key, for choosing the right environment for the right task.

Note that in this example we initialize with a constraint to use python v3.7 ... but your projects could be needing 2.7 or other 3.x versions of Python's Interpreter.

> conda activate pytorch1_py37

Your terminal should now show the activated conda environment

(pytorch1_py37) ~ > 

If you ever want to list your conda environments you can use

~> conda env list

# conda environments:
#
base                  *  /Users/myusername/anaconda3
py37                     /Users/myusername/anaconda3/envs/py37
pytorch1_py37            /Users/myusername/anaconda3/envs/pytorch1_py37

Picking the right PyTorch for deep learning

PyTorch is a deep learning framework that, as of today, supports CUDA to accelerate computation. If you want, you can elect to install it in CPU only mode as well.

PyTorch Install : https://pytorch.org/get-started/locally

At the URL above, you need to select the version of PyTorch, your OS, which package manager (We want Conda, a name that is short for Anaconda which we installed earlier)

Sample MAC OSX installation line looks something like

(pytorch1_py37) ~ >  conda install pytorch torchvision -c pytorch

Verifying installation

After you've executed the above commands, you'd now have a conda environment that is installed with all the requirements for PyTorch... it should have something at the end that looks like...

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(pytorch1_py37) ~ > 

To verify PyTorch is up and available, start Python in this terminal and perform the following code inside the python REPL.

from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
(pytorch1_py37) ~ > python --version
Python 3.7.3
(pytorch1_py37) ~ > python
Python 3.7.3 (default, Mar 27 2019, 16:54:48) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from __future__ import print_function
>>> import torch
>>> x = torch.rand(5,3)
>>> print(x)
tensor([[0.3981, 0.6907, 0.3435],
        [0.2003, 0.9270, 0.9716],
        [0.8785, 0.1334, 0.6019],
        [0.3291, 0.8080, 0.6652],
        [0.1482, 0.7221, 0.8023]])
>>> 

That last print(x) will contain a randomly initialized matrix, so don't expect yours to exactly match the example. If you've done everything above, and see it execute. You've finished!