Setting Up a New PyTorch Deep Learning Environment | by Mikhail Klassen | Towards Data Science

Whether you’re starting on a fresh project, or running on a remote machine, you don’t want to waste time chasing down dependencies and installing software libraries.
This tutorial will provide one of the fastest ways to get set up from a blank slate.
I have tested this approach by completing it on a plain no-frills EC2 instance running on Amazon Web Services.
Deep learning workflows in PyTorch are written in the Python programming languages, and you will find yourself needing to install many additional Python packages to get all the functionality you need as a data scientist.
To keep things clean, you need a tool that will help you manage your Python environment. Conda is powerful tool for managing packages, dependencies, and environments. We will install this first.
Miniconda is a free minimalist installer for conda. Find the right Miniconda for your operating system.
I’m using Python 3.8 for 64-bit Linux:
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
The above command downloads a bash script, which we now run:
$ bash Miniconda3-latest-Linux-x86_64.sh
You will need to review the license agreement. Press <space> to page through to the bottom, where you’ll type yes to agree to its terms.
Accept and install. You may need to accept or modify a few more settings during the installation process, but this should be straightforward. You may receive this warning:
WARNING:You currently have a PYTHONPATH environment variable set. This may cause unexpected behavior when running the Python interpreter in Miniconda3.For best results, please verify that your PYTHONPATH only points to directories of packages that are compatible with the Python interpreter in Miniconda3: /home/klassm/miniconda3
The PYTHONPATH environmental variable tells your system where to look for Python and applicable packages. You can check what directory this has been set to. On Linux:
$ echo $PYTHONPATH
After listing that directory’s contents, I saw that it was only used for a small number of system-wide site packages. I decided it was safe to continue without reassigning my PYTHONPATH variable.
Exit and return to reset the shell. Conda has set you up in its ‘base’ environment.
Now the conda app is installed and available to me from the command line. I can verify this by typing
$ conda
in the command line. You should see output that looks like the following:
usage: conda [-h] [-V] command ...conda is a tool for managing and deploying applications, environments and packages.Options:positional arguments:...
Now let’s create a virtual environment for deep learning. It’s best practice to create a virtual environment for every project. This way you can separate all the dependencies for each project. Even if you think you’ll re-use the same Python packages, sometimes you’ll need different versions of the same libraries. Things can quickly get out of hand. Do yourself a favor and just create a virtual environment.
The following instructs Conda to create a virtual environment called “pytorch”:
$ conda create -n pytorch
Conda will let you know what it plans to do and ask if you agree. Agree to proceed. You should see something like this:
## Package Plan ##environment location: /home/ec2-user/miniconda3/envs/pytorchProceed ([y]/n)? yPreparing transaction: doneVerifying transaction: doneExecuting transaction: done## To activate this environment, use##     $ conda activate pytorch## To deactivate an active environment, use##     $ conda deactivate
Activate the pytorch environment with
conda activate pytorch
You’ll notice how “(base)” changes to “(pytorch)” in the command line, informing you that you’re now in the new virtual environment.
This next step is optional. If you don’t intend to run on GPUs, you can skip ahead.
If you plan on training large neural networks on large data sets, GPUs are almost essential. Because of how their architectures differ from CPUs, GPUs are ideal for running large parallel matrix calculations. Incidentally, training a neural network is mostly matrix calculations.
CUDA is a library by Nvidia that allows developers to interface with GPUs. Since the installation instructions will vary by operating system, follow the instructions on Nvidia’s installation guide.
Now install the Python packages you’ll need. Matplotlib is useful for creating plots of your data, but not strictly necessary.
The following command will install these packages and their dependencies. This took up about 1 GB of space on my machine.
conda install -c conda-forge matplotlib pytorch torchvision
Depending on your project, you may want some other Python libraries such as scikit-learn or pandas. These you can easily install in the same way.
Let’s make sure that everything installed correctly. Open a Python shell and type:
>>> import torch>>> x = torch.rand(5,3)>>> print(x)
This creates a tensor with 5x3 dimensions filled with random values. The output should look something like this:
tensor([[0.1279, 0.4032, 0.7045],        [0.2979, 0.1128, 0.7660],        [0.9651, 0.1172, 0.2716],        [0.9355, 0.1351, 0.3755],        [0.3469, 0.9118, 0.0110]])
Congratulations! You’re all set up to run deep learning workloads in PyTorch.
So this tutorial can give the impression that setting up a deep learning environment is easy, and it can be. However, the hard/lengthy part is setting up CUDA correctly.
The easiest way to avoid any headaches is to start from a preconfigured deep learning environment. There are some great resources that do the heavy lifting for you.
On AWS, you can launch your EC2 instance from an Amazon Machine Image that comes pre-built with all the necessary deep learning libraries and CUDA. Just search “deep learning” when selecting your AMI and you’ll find several options.
Virtual machines preconfigured for deep learning are available on Google’s Cloud and Microsoft Azure as well. Use whatever cloud you like best.
When choosing which type of machine to run on, remember to select an instance that has attached GPUs, if you intend to leverage them. Don’t forget to shut down your instances when you’re done with them! These machines tend to be expensive to run.
If you’d rather just run on your own machine, another option is to use an all-in-one pre-built Docker image. Check out Deepo for some examples. If you have a powerful rig at home, this may be the more cost-effective option.