Installing Keras with Theano on Windows for Practical Deep Learning For Coders, Part 1

The below instructions should have you set up with both Keras 1.2.2 and theano 0.9 on Windows 8.1 in 30 minutes or less, depending on the speed of your internet connection. I can confirm that this set up is suitable for all the lessons in the fantastic Practical Deep Learning For Coders, Part 1, course.

The only requirements are:

Providing you have a suitable Nvidia GPU and you have installed both Visual Studio Community 2013 and the CUDA 8.0 Toolkit you are ready to proceed onto the next steps.

Install Anaconda 2 and bcolz
  1. Install Anaconda 2 to your user directory. If you have spaces in your user directory name, then I would suggest installing it somewhere with no spaces in the path, for example C:\Anaconda2\
  2. Start the Anaconda Prompt, install bcolz (for use in the course) and then update all packages.
    conda install -y bcolz
    conda upgrade -y --all

Install Nvidia cuDNN
  1. Extract the contents of the archive to its current directory.
  2. This should extract to a cudnn-8.0-windows7-x64-v5.1 directory inside the current directory. Copy the contents (bin, include and lib directories) from
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\.
    This will copy all the required cuDNN files into the bin, include and lib directories, inside your CUDA 8.0 install.

Install theano 0.9 and verify that it is using the GPU
  1. Install dependencies
    conda install -y numpy scipy mkl-service libpython m2w64-toolchain nose nose-parameterized sphinx pydot-ng
  2. Install theano and pygpu
    conda install -y theano pygpu
  3. Create a .theanorc file in your user directory (e.g. C:\Users\USER_FOLDER_WITHOUT_SPACES\.thanorc) and add the following to
    device = gpu
    floatX = float32
    compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
    enabled = True
    include_path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
    library_path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64

    The easiest way to do this, because windows explorer won’t let you create a .theanorc file, is to open up notepad, copy the above text and save with “Save as type” set to “All Files (*.*)”, alternatively you can download from here.
  4. Test your install is using the GPU by running the code provided by the theano team. The output when run through the anaconda prompt should resemble the below, to verify that theano is using the GPU you want to see the text “Used the gpu”.

Install and test Keras 1.2.2

The instructions below will install an older version of Keras, the current version at present is 2.0.6. 1.2.2 is the version which the course was constructed to use, and hence the most widely tested. If you want to install the latest version instead then skip this section and go to Installing the current version of Keras.

  1. Install Keras 1.2.2
    pip install keras==1.2.2
  2. Create a .keras directory inside your user directory (e.g. C:\Users\USER_FOLDER_WITHOUT_SPACES\.keras). The easiest way to do this, because windows explorer won’t let you create a folder starting with a “.” is from the command prompt:
    • Press the windows key + r and type in cmd.
    • This will land you in your user directory so all you need to do is create the directory by typing
      md .keras
  3. Create a keras.json file inside the newly created .keras folder and add the following.
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "theano"

    Alternatively you can download the .keras folder from here.
  4. Test your Keras install with the training example shown below.
    from keras.models import Sequential
    from keras.layers import Dense, Activation
    import numpy as np# For a single-input model with 2 classes (binary classification):
    model = Sequential()
    model.add(Dense(32, activation='relu', input_dim=100))
    model.add(Dense(1, activation='sigmoid'))

    # Generate dummy data
    data = np.random.random((1000, 100))
    labels = np.random.randint(2, size=(1000, 1))

    # Train the model, iterating on the data in batches of 32 samples, labels, epochs=10, batch_size=32)

    If everything is installed correctly the output from the above code should be similar to the below.

    Start the Practical Deep Learning For Coders, Part 1 course
    1. Download or clone the course material to your user directory.
    2. Type jupyter notebook in the Anaconda prompt and enjoy the course.

    Installing the current version of Keras

    The latest version of Keras is not guaranteed to work with all the lessons from the Practical Deep Learning For Coders, Part 1 course, however with the changes below I have not had any errors, only warnings regarding API changes.

    1. Install the latest version of Keras.
      pip install keras
    2. Change the following in
      • from keras.regularizers import l2, activity_l2, l1, activity_l1
        from keras.regularizers import l2, l1
      • from keras.utils.layer_utils import layer_from_config
        from keras.layers import deserialize as layer_from_config
      • return np.concatenate([ for i in range(batches.nb_sample)])
        return np.concatenate([ for i in range(batches.n)])
    3. Change the below in your keras.json file
      "image_dim_ordering": "th"
      "image_data_format": "channels_first"
    4. The number of steps in calls to fit_generator, predict_generator and evaluate_generator has changed from the number of images to the number of batches. For example in lesson 3 where you previously had
      trn_features = conv_model.predict_generator(batches, batches.nb_sample)
      you would now put
      trn_features = conv_model.predict_generator(batches, batches.n/batches.batch_size)

    If you have any comments and/or problems then please let me know.

    2 thoughts on “Installing Keras with Theano on Windows for Practical Deep Learning For Coders, Part 1

    1. Dear James,
      First of all, thank you for publishing such useful instructions. In the course, Practical Deep Learning For Coders, Part 1, is mentioned that they are using Python 3. I would like to know if I can follow the same instruction to install Python 3.

      1. Hi Marco,
        The v2 of the course uses pytorch and python 3 which should be even simpler to configure on windows, I would check out the forums to see if someone has written some installation instructions. I wrote this guide so people could follow v1 of the course on windows.
        I think the main steps would be to install annaconda 3, then to get pytorch on windows
        conda install -c peterjc123 pytorch
        then the lib
        pip install fastai
        Then when you first run the notebooks you may find there are a couple more missing packages which are not available directly through conda as they are in linux, but these should be easy to track down.

    Leave a Reply

    Your email address will not be published. Required fields are marked *