Thursday, March 9, 2017

Setting up TensorFlow with CUDA on Windows

I did the post about How to setup TensorFlow on Windows about a month back. I only covered setting up the CPU version of TensorFlow there, and promised that I'll do the guide for the GPU version soon. But I haven't had the change to come round to write the guide until now.

I guess better late than never. So, here's how to setup the GPU version of TensorFlow on Windows.

So, what do you need to get TensorFlow working on GPU?
You need to setup the following prerequisites, in that order.
  1. Microsoft Visual Studio 2015 (The free community edition of VS 2015 will work)
  2. CUDA 8.0
  3. cuDNN 5.1 for CUDA 8.0
Start by installing Visual Studio 2015. You can get the free community edition from here. When you install, make sure to select ‘Custom Installation’, and select ‘Visual C++’ in the programming language selection (by default, C++ is not selected). Once installed, check whether you have C++ capability by checking the ‘New Project’ options.

Visual Studio 2015 installed with C++
Visual Studio 2015 installed with C++


Then, get CUDA 8.0 and cuDNN 5.1 for CUDA 8.0 (click on the links to go to the downloads pages).

Downloading CUDA 8.0
Downloading CUDA 8.0

Downloading cuDNN 5.1 for CUDA 8.0
Downloading cuDNN 5.1 for CUDA 8.0


Install CUDA 8.0 first. When installing, select the custom install and deselect the GeForce Experience and Display Driver if you already have the driver installed.

Deselect the GeForce Experience and Display Driver components
Deselect the GeForce Experience and Display Driver components
Once CUDA is installed, extract the contents of cuDNN into the CUDA installation directory. The contents of the 'bin', 'include', and 'lib' folders should go to the folders with same name.

Extracting cuDNN
Extracting cuDNN

Now you are ready to install TensorFlow.

Start by creating an Anaconda environment for TensorFlow and activate it.
 conda create --name tensorflow numpy scipy scikit-learn pillow h5py mingw libpython   
   
 activate tensorflow  

Then, we install TensorFlow GPU package from pip. We install it in 2 steps to avoid messing up the conda installed packages (explained in my earlier post)
 pip install --upgrade --no-deps https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.0.1-cp35-cp35m-win_amd64.whl    
      
 pip install https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.0.1-cp35-cp35m-win_amd64.whl  

Let's run a sample TensorFlow commands to see if the installation is successful.
 python  
 >>> import tensorflow as tf   
 >>> hello = tf.constant('Hello, TensorFlow!')   
 >>> sess = tf.Session()   
 >>> print(sess.run(hello))   
 Hello, TensorFlow!   
 >>> a = tf.constant(10)   
 >>> b = tf.constant(32)   
 >>> print(sess.run(a + b))  

If all goes well, you will see that TensorFlow recognizes your GPU and create a TensorFlow device on it.

TensorFlow running on GPU
TensorFlow running on GPU
Now you're ready to run TensorFlow models on GPU, on Windows.

Related Posts:
Setting up TensorFlow on Windows

No comments:

Post a Comment