Ankivil

Machine Learning Experiments

Software

Installing Keras, Theano and Dependencies on Windows 10 – Old way with Python 3.4

Introduction

[UPDATE: Check my improved tutorial on how to install Keras & Theano which doesn’t require to downgrade to Python 3.4]

In the previous post, Choosing a Deep Learning Software, Theano with Keras were found to be the most adapted Deep Learning software for neophytes using Windows.

There are several ways to install Keras and Theano, this article presents what seems to be the easiest procedure, but there is certainly ways to optimize this process.

Installing CUDA

This step is optional but highly recommended for NVidia card owners as it massively speeds up Theano. So, for Theano to be able to use the GPU, we must install CUDA. First, look at the CUDA system requirements to determine which CUDA and Visual Studio version you will be installing. I chose the lastest version of CUDA (7.5) so the compatible Visual Studio version is the 2013. Note that you should NOT install a newer Visual Studio version, in my case VS 2015 was not compatible with CUDA 7.5.

  1. Download and install Visual Studio Community 2013 (bottom left on the page) don’t install web and silverlight stuff if you’re not planning to use them. Open to see if everything’s OK.
  2. Download and install CUDA 7.5, choose “Express” installation. Open the samples directory and then the Samples_vs2013.sln file. Run any project to see if CUDA is working like, for instance, the 5_Simulations/oceanFFT project. Right click on it and then Debug>Start new instance, accept to build and then you should see a simulated ocean surface.
  3. Add C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin to the PATH. Theano won’t need it because it will use a configuration file but it will be useful to test the configuration later.

Installing Theano

  1. Download and install Anaconda for Python 3.5. If you install it for all users (like I did) be sure to open all following consoles in admin mode (right click on start button -> Command Prompt (admin)). I installed Anaconda in Program files\Anaconda, make sure you replace this path by your installation path in the next commands.
  2. Because libpython in not compatible (for now) with python 3.5, we need to downgrade to python 3.4. In the command line, type the commands:
  3. Download and install Git for Windows.
  4. We will install the bleeding-edge version of Theano from github. In the command line, type the commands:
  5. Open the explorer and type in the address bar %USERPROFILE%, open the .theanorc file and add the following lines:

    You should note that in the nvcc flags, spaces are not handled so you have to replace them with “=”. I assume you installed CUDA, if it is not the case, you have to replace device = gpu by device = cpu . If you have more than one GPU, you can also specify which one you want to use using gpu1 or gpu2 etc…
  6. If you installed CUDA, download PyCUDA, in my case the right file was pycuda-2016.1+cuda7518-cp34-cp34m-win_amd64.whl because of Python 3.4. Then, run
  7. Now it’s time to test if Theano is working properly. Copy the following code into a file and run it in the command line.

    If everything’s OK, this code will display something like that:
  8. If you have installed CUDA, you can also test pyCUDA using this code:

    If everything’s OK, this code will display a matrix full of zeros.

Installing Keras

  1. We will also install the bleeding-edge version of Keras. The procedure is similar to the one for Theano:
  2. Then it’s time to test Keras using this code:

    If everything is working properly you should see:
  3. To be able to draw graph representation of models we will build, there are two more dependencies to install. Open the command line (you may need admin rights) and enter:

Conclusion

Now you should be ready to do amazing stuff with Keras and Theano. If you want to make your networks run faster, you should check my article on how to use CuDNN and CNMeM with Theano.

References

21 Comments

  1. Jenny

    Hi, I followed your steps to get theano to run on GPU, but it stills tells me it’s running on CPU. Do you know if the CUDA 7.5 comes with the driver or just the toolkit? I think it’s not detecting any GPU hardware on my computer.

    Thanks

    • Fabien Tencé

      Hi Jenny,

      To my knowledge, CUDA is only the toolkit [edit: after checking, the CUDA installer also installs drivers], you have to have an up-to-date driver for your NVIDIA card. You may also check that your OS and your card are compatible with CUDA, see here.

      Did you try to run an example code from the toolkit? I mention the oceanFFT project in my post, if it’s working then it must be a problem with the configuration of Theano. In that case, double check that the .theanorc file is present in the right place with the right flags. If you have several GPUs in your PC, you may also look at the documentation and change the device = gpu line for device = gpu1 or gpu2 or gpu3 or even gpu0 in any case.

      Good luck.

  2. Yoel Shoshan

    Hello Fabien,
    Thank you so much for this great tutorial!

    Few notes that may assist few people:

    1. I verified that Cuda samples are working well, but still had issues with setting compiler_bindir=blahblah. Kept getting nvcc error messages. Considered moving to a different flag (after seeing what was used in Cuda samples “-ccbin “).

    Before I tried it, simply removing the compiler_bindir entry from .theanorc did the trick and everything worked 🙂

    2. When trying to visualize the graph I got a message that pydot is not installed. Turns out that you also need to install graphviz library itself (from here http://www.graphviz.org/Download_windows.php).
    I think that conda just installs a python wrapper.

    3. For some reason, I only see the captcha on chrome browser, and can’t see the captcha (and comment here) on Firefox.

    Thanks again for the great tutorial 🙂

    • Fabien Tencé

      Hi Yoel,

      Thank you for this information, it will certainly help others installing these software and libraries. It seems that the whole process is very sensitive to each setup and each version. I may also have forgotten the graphviz part in the installation, I’ll have to check that.

      The captcha needs javascript to work, that may be the problem. Anyway, I’ll have to change that, I installed the captcha pluggin only as a temporary fix to spam comments.

      [edit]
      1. After writing the improved tutorial, it seems that nvcc flags should not be set depending on the values in your PATH.
      2. You do not need to install GraphViz, conda install graphviz is already taking care of that. However, pydot may not find the GraphViz executables (well, the batch files), see the relevant section in the improved tutorial
      [end edit]

  3. Thank you so much !! Clear and Perfect tutorial !!!

  4. Michael Felsberg

    Downgrading to 3.4 breaks all GUI stuff. PySide conflicts with 3.4.

    • Fabien Tencé

      Hi Michael,

      I find it annoying too that we have to downgrade to 3.4 but I couldn’t find any workaround. If anyone finds a solution, I’d gladly update my article.

    • Fabien Tencé

      You can check my new tutorial as it doesn’t require the downgrade to Python 3.4.

  5. Kevin

    Hi Fabien, thanks for the guide

    I am getting “nvcc fatal : nvcc cannot find a supported version of Microsoft Visual Studio. Only the versions 2010, 2012, and 2013 are supported” when I run the test script. It seems like this isn’t the real issue becuase the arguments ‘–compiler-bindir’, ‘”C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin”‘, are in the list of commandline arguments for nvcc. So it seems like nvcc should know where the msvc compilers are. The OceanFFT sample also works, so it looks like my gpu works with cuda. Any ideas?

    Thanks again, this was an excellent guide. I honestly haven’t been able to find a better one

    • Kevin

      I’ve tried this guide:
      http://simranmetric.com/installing-theano-on-windows-10-python-3-5/
      and used some information from here as well:
      http://rosinality.ncity.net/doku.php?id=python:installing_theano

      It seems like if you use mingw you still have to install TDM for its include files (use mingw g++ and include files/headers from TDM) based on information I found here:
      https://github.com/Theano/Theano/issues/4778
      (I had the same issue that person was having about hypot not being declared, I found something about how to fix it here via a commandline argument to change the definition of hypot:
      http://insertinterestingnamehere.github.io/posts/first-impressions-and-overcoming-compilation-errors.html).

      I still haven’t gotten it to work. I think it at least compiles now, but I’m getting some kind of runtime error. Here’s my compile command if that helps:
      C:\Program Files\mingw-w64\x86_64-6.2.0-posix-seh-rt_v5-rev1\mingw64\bin\g++.exe -shared -g -D_hypot=hypot -shared -I”C:\Program=Files\TDM-GCC-64\include” -I”C:\Program=Files\TDM-GCC-64\x86_64-w64-mingw32\include” -DMS_WIN64 -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -m64 -DMS_WIN64 -IC:\Program Files\Anaconda3\lib\site-packages\numpy\core\include -IC:\Program Files\Anaconda3\include -IC:\Program Files\Anaconda3\lib\site-packages\theano\gof -o C:\Users\Kevin\AppData\Local\Theano\compiledir_Windows-10-10.0.14393-SP0-Intel64_Family_6_Model_61_Stepping_4_GenuineIntel-3.5.2-64\lazylinker_ext\lazylinker_ext.pyd C:\Users\Kevin\AppData\Local\Theano\compiledir_Windows-10-10.0.14393-SP0-Intel64_Family_6_Model_61_Stepping_4_GenuineIntel-3.5.2-64\lazylinker_ext\mod.cpp -LC:\Program Files\Anaconda3\libs -LC:\Program Files\Anaconda3 -lpython35

      I am getting undefined reference / import errors now

    • Kevin

      Actually, it is definately not a runtime error. I missed the following line before the command:

      Problem occurred during compilation with the command line below:

      So it is still trying to compile, but the errors look like they are coming from Python

      • Fabien Tencé

        Hi Kevin,

        To be honest, I don’t have a clue how to solve your problem. I’ll have to try the installation process for python 3.5 but I have first to reinstall Windows to be sure I don’t miss any dependency. So stay tuned for updates to this article, hopefully before the end of September.

  6. Moon

    I’m trying to learn python here as well so forgive me if this is a dumb question. But I’m getting syntax errors when I run your code to test Theanos. Specifically, it’s this line:

    “iters = 1000rng = numpy.random.RandomState(22)”

    It doesn’t like the 1000rng part. Am I missing a package? Or is there a typo somewhere?

    • Fabien Tencé

      Hi Moon,

      You’re right, it’s a typo, sorry for that. The indentation was also wrong but it is now corrected.

      • Moon

        Thanks! I caught another typo in the pycuda example. The third line should be split.

        Also, I think there might be a typo in the keras example. I think nb_classes should be different? I get a type error on line 16 that it needs to be an integer type.

        In case this is of any help to others, I was able to follow the tutorial given in the link by Michael. I was able to get Theanos to work with python3.5 after following it but I was unable to get keras to work. Eventually, I ended up following your tutorial here except I also had to leave out the “compiler_bindir” line in the .theanorc file.

        Downgrading to python3.4 did kill the GUI applications that came with anaconda. However, I was able to regain Spyder by running “conda update spyder” (not sure if this was necessary) and then following the instructions by timctran in this thread: https://github.com/spyder-ide/spyder/issues/2858. This is all with Windows 10.

        • Fabien Tencé

          Hi Moon,

          Well, there were typos too on both script, again sorry for that. They should be OK now.

          Thanks for your feedback on the other tutorial, I’ll try it after my next article. I’ll double check all my scripts and Theano flags while I’m at it.

          If there are some broken conda package during the downgrade, you should use conda install the_package to downgrade it too. If you need python 3.5 and 3.4 running on your machine (3.4 for Keras, 3.5 for other stuff), you should use conda environments.

  7. Well, thanks for this EXCELLENT tutorial!! Here are some minor remarks after successful completion, which may be also useful for other readers:

    1) I used Windows 7

    2) I didn’t need to downgrade my existing (Anaconda) Python 3.5 installation; instead, I created a new conda environment with Python 3.4 and all the necessary packages

    3) As already noted in some comment above, the line

    compiler_bindir=”C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin”

    in .theanorc kept giving nvcc fatal errors ; removing it resolved the issue.

    4) Line 24 in your pyCUDA test script should be

    print(dest-a*b)

    i.e. with parentheses – it’s Python 3 here 😉

    Great work & thanks again!!

    • Fabien Tencé

      Hi Christos,

      I’m glad it worked on Windows 7 too. You’re right, using an Anaconda environment may be the best solution to keep everything clean.

      I fixed the parentheses, thanks for the heads up.

  8. Wittynerd

    I’m using Anaconda2 which has python2.7
    While installing theano i.e, when I give command
    python setup.py develop ,I get a error message

    ImportError: No module named numpy.distutils.core

    what’s the possible fix?

Leave a Reply

Theme by Anders Norén