Installing Keras, Theano and TensorFlow with GPU on Windows 8.1 and 10 in less than 4 hours


If you want to install the main deep learning libraries in 4 hours or less and start training your own models you have come to the right place. I created this tutorial to help you quickly and easily install the most complete libraries for deep learning such as Keras, Theano, and TensorFlow from scratch with GPU. In Linux the process is likely very similar but I have not tested it. 

This step by step tutorial will install Keras, Theano and TensorFlow using CPU and GPU without any previous dependencies. When you finalize this tutorial you will be able to work with these libraries in Windows 8.1 or Windows 10. In this tutorial we will be not be using the latest version of the programs but instead the most recent configuration that works for the last deep learning libraries. Visual Studio 13 (not visual studio 15), Python 3.5 (not Python 3.6), Anaconda 4.2 (not 4.3.1) , CUDA 8.0, and cuDNN v5.1 (not 6). If you install exactly these dependencies Keras, Theano and TensorFlow will work perfectly. 

Install Visual Studio 13
  1. Download and install Visual Studio Community 2013 update 5.
  2. Add the route of VS2013 to your PATH  C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin using the environment variables (I use Rapid Environment Editor but you can do it manually)
Install CUDA 8.0
  1. Download and install CUDA 8.0
    1. If you have problems compiling CUDA. It could be because you need to install the DirectX SDK
  2. Test Cuda 8.0 has been correctly installed. Run any  of the examples from CUDA in C:\ProgramData\NVIDIA Corporation\CUDA Samples\v8.0 you can run them using Samples_vs2013.sln
Install Anaconda3 4.2 with Python 3.5
  1. Download and install Anaconda3 4.2 with Python 3.5
  2. Check that Python and Anaconda are correctly installed. Open a command terminal (cmd) and execute python, the version has to be Python 3.5.2, you can test if it works correctly (3+5=8)
Install Theano
  1. Open a command terminal (cmd) and execute: C:\>conda install theano, say yes to all the dependences.
  2. Install the compiler for Theano: C:\>conda install mingw libpython  
    1. I had already installed MinGW x86_64-5.4.0-release-posix-seh-rt_v5-rev0.7z, if at the end Theano doesn't work try to install it, and add the path where you unzip the MinGW to the PATH (environment variables)
  3. Execute in command terminal C:\>conda list theano to know the version, it should be 0.9.0
  4. Test Theano.
    1. Create a file named with the following code:
      import theano
      a = theano.tensor.vector()      # declare variable
      out = a + a ** 10               # build symbolic expression
      f = theano.function([a], out)   # compile function
      print(f([0, 1, 2]))
    2. Execute in a command terminal (cmd) C:\pathfile\>python
         Result: [    0.     2.  1026.]
Install TensorFlow cpu version
  1. Open a command terminal (cmd) and execute: C:\>pip install tensorflow
Install Keras
  1. Open a command terminal (cmd) and execute: C:\>pip install keras
Test Keras, Theano and TensorFlow-cpu
  1. Download deep learning script example from Keras, it will use a CNN to classify the database Cifar10. Change "epochs = 200" to "epochs = 2" in order to do a fast test
  2. Test Keras with TensorFlow-cpu. In a command line execute: C:\pathfile\>python . Keras is by default using TensorFlow backend
  3. Test Keras with Theano
    1. Save Keras configuration file using TensorFlow as backend, we will use it again later for testing the TensorFlow-gpu version
      1. Save file keras.json in  C:\Users\nameUser\.keras\ as kerasTensorFlow.json, where "nameuser" is the name of the user
    2. Change the backend to Theano. In keras.json file write:
          "floatx": "float32",
          "epsilon": 1e-07,
          "image_dim_ordering": "th",
          "backend": "theano"
    3. Create configuration file for Theano .theanorc in C:\Users\nameUser\
      [global] floatX = float32 device = gpu [nvcc] compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
    4. In a command line execute: C:\pathfile\>python . Keras is by default using Theano backend now. In the first line after the Keras python script it will tell you the backend it is using.
  4. If you compare both you will see as Theano is faster because it's using gpu, you can change it if you want in device (device = cpu) to see the difference in the speed.
Install cuDNN v5.1
  1. Download and install cuDNN v5.1
    1. The gpu version of the TensorFlow needs CUDA 8 and cuDNN v5.1. You will need to register in nvidia to download it
    2. Download for windows 8.1 users and version for windows 10 users.
  2. Unzip the downloaded file and copy the bin, lib, and include files in their respective folders in  C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\ 
Install TensorFlow gpu version
  1. Open a command terminal (cmd) and execute: C:\>pip install tensorflow-gpu
Test Keras with TensorFlow-gpu
  1. Rename the configuration file for Keras using TensorFlow as backend kerasTensorFlow.json as keras.json in  C:\Users\nameUser\.keras\
  2. Execute again in a command line: C:\pathfile\>python Keras is now by default using TensorFlow-gpu
If you have followed all the steps, your machine is now able to work with Keras and Theano, Tensorflow in CPU and GPU mode. Now you can start training and testing your own models. Let me know if you were able to do it in under 4 hours !!