Getting Started with Tensorflow

The goal

The goal of this post is to outline some steps to get a stable, working, reusable (python) environment to use the (nVidia) GPU accelerated version of Tensorflow on (Arch) Linux with aim of building scientific computing/machine learning projects.

Please excuse the grumpy undertones that come with spending 1.75 days setting up any new programming environment you're not familiar with :-)

My previous experience with Python

... is pretty much none. I've never used python much. When I was first introduced to it, many years ago, it was right when there was a recent update in 2.x series that didn't seem handle backwards compatibility all that well. Libraries used seemed to be for older versions than the new examples were written for... pip was still a couple years away... it seemed the (linux managed) system installed version was always 'too up-to-date' to work with anything useful already written... and since I was working a lot with Fortran (77) code, I had my editor set to map tabs to seven spaces, ensuring disaster when editing any code of someone else's. My intro to python didn't go well, and I never had reason to go back.

Not until now.

Revisiting the world of python, it seems little has changed. My wonderful Arch Linux distribution gives me the latest and greatest python version, 3.6. Oh, and 2.7, because the 3 series isn't backwards compatible. You still have to be cognoscente of libraries your project will use to make sure you're not in for big refactor later. It seems that people have developed some handy things to get around this though: such as virtualenvs to set virtual environments where you can work with versioned packages in one python branch, without worrying about an update breaking compatibility somewhere. There also seems to be a handy thing called Anaconda, which has virtual environments, but geared towards scientific computing (and more languages than just python).

You're going to have to make some tough decisions early on about what method you want to use. Python virtualenvs... anaconda... no managed environments at all... Here's what I decided on:

  • Use (mini)conda for handling virtual environments (as opposed to virtualenvs)
  • Use pip to install essentially everything else python related (as opposed to conda)

Things you will need

I just want to touch on things not provided by your handy-dandy Linux package manager, though don't forget cuda (version 8) -> pacman -Sy cuda

Here is the link to the official Tensorflow installation instructions page, which also points out the nVidia requirements!

  • cuDNN libary (requires registration) The prebuilt versions of Tensorflow use the 5.1 version.
  • miniconda (see section below)
  • Tensorflow 1.x URL for whatever python version you will be using. I'm using 3.6. This is a backup, as hopefully we can install it without the URL (see section below).

Miniconda

Anaconda contains a few gigs of pre-installed scientific computing packages. Miniconda is a small "bootstrap" version that includes only conda, Python, and the packages they depend on. The most useful thing I see for my use case on conda, is the virtual environments for things not just python. For example, I have the several versions of Nvidia cuDNN (CUDA Nerual Network Library) on my pc. I can get version 6 from the AUR, but the version of tensorflow downloaded works with 5.1. With a conda environment, there is a separate /includes and /lib directory where I can deposit these types of versioned libraries and not have to worry about it!

Bonus: Since things get installed user-level, you don't have to worry about package/installation permissions.

Let's live life on the edge, and download their 3.6 Installer.

Note from their website: There are two variants of the installer: Miniconda is Python 2 based and Miniconda3 is Python 3 based. Note that the choice of which Miniconda is installed only affects the root environment. Regardless of which version of Miniconda you install, you can still install both Python 2.x and Python 3.x environments.

Once installed, go ahead and make your environment, and switch to it

conda create -n tensorflow

source activate tensorflow

Pip

My first two run throughs trying to set up an environment for tensorflow, I used conda to install things, but then realized that I had to install the GPU version of tensorflow with pip, but keras (via conda) depended on a CPU version of tensorflow. After seeing multiple dependencies being saved for various packages after a while, and even one dependency that depended on a different version of python than set in my environment, I asked myself "Why not just download everything via pip... It's all managed in my conda environment anyways?". So I did. There was much rejoicing.

Very important note about pip in a conda environment.

pip isn't installed by default in a new environment. If the conda pip is in your path, which is likely, then anything you install via pip will (accidentally) be installed in the root conda environment (as opposed to our tensorflow environment). It only took me the better part of a day to notice, and then find an issue thread on github that pointed this out. This pretty much negates the whole reason of having a virtual environment. The first thing you should do in your new environment is

conda install pip

or get in the habit of specifying it as a dependency when you create your environment, e.g.

conda create -n tensorflow pip

Now you know why this section started of with "My first two run throughs..."

Tensorflow

In retrospect, this part seems pretty simple now. Since we have a conda environment, using python 3.6, with the local environment version of pip installed... we should only have to do

pip install --upgrade tensorflow-gpu

and the appropriate tensorflow should be installed. Otherwise, you can replace tensorflow-gpu with the URL of the package given from the link in Things you will need

cuDNN

Register with nVidia and download the 5.1 versions of the library from the link in Things you will need. Once you unzip it, copy the files from /cuda/include to the /include directory of your conda environment, and the same for the files in /cuda/lib64 to /lib of your conda environment.

Test it out

Now, just fire up python and try "import tensorflow". If you see

I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally

Then you have cuda installed, and got the cuDNN library in the right place! If you see the following in there

I tensorflow/stream_executor/dsoloader.cc:126] Couldn't open CUDA library libcudnn.so.5. LD_LIBRARY_PATH: I tensorflow/stream_executor/cuda/cudadnn.cc:3517] Unable to load cuDNN DSO

then it can't find the cuDNN library files. Double check and make sure you have the files in the right place, and the right version!

Now, head on over to their tutorials page and test this sucker out! (You'll likely need to install some other packages, such as ipython, via pip)

Less grumpy summary

  1. Visit the official install page to find out what version of cuda/cuDNN you will need (8/5.1 for python 3.6 in my case as of writing this)
  2. Ensure you have the cuda library installed (pacman -Sy cuda)
  3. Register and Download the 5.1 cuDNN library from nVidia's site
  4. Download and install miniconda3
  5. Start a new conda environment that has it's own pip via conda create -n tensorflow pip
  6. Copy the cuda/include/ and cuda/lib64/ files for the cuDNN library to your conda environment's /include and /lib folder
  7. Switch to your tensorflow environment via source activate tensorflow
  8. Install tensorflow via pip install --upgrade tensorflow-gpu
  9. Learn from the official tutorials!
  10. Work on your own cool project :-)