Thursday, April 9, 2015

Tutorial: Python 3.4, Theano, and Windows 7&8-64bit

Hello Everyone,

Update: I wrote a new post that uses Anaconda instead of installing Mingw directly. This way you can keep the path variable clean and makes installing numpy/scipy much easier. http://www.islandman93.com/2016/04/tutorial-theano-install-on-windows-7-8.html

I'm in love with Python and I always use the latest version of everything so I'm stuck with 3.4. I use Windows for reasons... This presents a problem for machine learning. Theano is not too easy to install on Windows.

I wrote this tutorial to help others get a working Python 3 version of Theano on a Windows 7 64bit PC. It's not meant for the faint-of-heart and will probably take a decent amount of time to download and install everything but in the end it's worth it.

Let's get to it:

  1. Make sure your computer has a compatible CUDA graphics card: https://developer.nvidia.com/cuda-gpus 
  2. Download CUDA 
    1. https://developer.nvidia.com/cuda-downloads (I downloaded Cuda 7.0.28)
  3. While that's downloading, head to https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx and get Visual Studio 2013 (the community version).
    1. Download and install, this will install the needed C++ compilers 
    2. Couple of notes here, my install needed 7GB and took ~20 minutes to install
    3. Restart
  4. Install CUDA ~7 minutes
    1. Note: Nsight won't install for older versions of Visual Studio if you don't have them, no worries
    2. I restarted this is windows after all...
  5. Check CUDA
    1. Navigate to C:\ProgramData\NVIDIA Corporation\CUDA Samples\v7.0\1_Utilities\deviceQuery and open the vs2013.sln file
    2. Use CTRL+F5 to run the device check and keep the cmd window open
    3. Make sure you Pass the test, otherwise there is a problem
  6. Download and install MinGW
    1. http://mingw-w64.yaxm.org/doku.php/download
      1. The Mingw-builds links are the easiest to use, link in next step.
    2. I got the Mingw-builds specifically
      1. NOTE if using the Mingw-builds install: Make sure you specify Architecture: x86_64, and Threads: win32
      2. Threads: posix may work but I didn't try it
      3. I also chose my destination folder as C:\mingw\ otherwise it installs to Program Files which has a space in it
    3. You can use the mingw of your choice there's alternative steps to take under number 11.
  7. Download Theano
    1. https://github.com/Theano/Theano, Download Zip at the bottom right
    2. Extract
  8. Dependencies
    1. Download and install Numpy, Scipy, and LibPython
    2. The best place for these is 
      1. http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
      2. http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
      3. http://www.lfd.uci.edu/~gohlke/pythonlibs/#libpython
    3. Get the win_amd64.whl files
    4. Use pip install numpyOrScipy.whl to install
  9. Open CMD prompt
    1. Navigate to Theano extracted folder /Theano-master
    2. Use python setup.py install
      1. This automatically uses 2to3 conversion
  10. We need to add some system variables
    1. Right click Computer -> properties -> advanced system settings -> environment variables
    2. Add a new system variable
      1. Name = THEANO_FLAGS
      2. Value = floatX=float32,device=gpu,nvcc.fastmath=True
    3. Also add Visual Studio's c++ compiler to the path
      1. Add ;pathToYourVSInstallation\VC\bin\
  11. Almost there, Open Mingw folder
    1. Run mingw-w64.bat this will open a mingw command prompt
      1. Alternative for different mingw: set Mingw as the first in the path variable.
    2. Type python and press enter
    3. Import Theano and you should be good to go!
      1. Alternative for different mingw: Change mingw to last in the path variable
    4. Test theano with theano.test()
Now you'll be able to use Theano under any environment. Use PyCharm, it's the best.

Let me know any questions or comments below.

15 comments:

  1. Hello IslandMan93,

    I followed most of the steps.

    The system passed the cuda test in step 5.

    But installation of Mingw from the given link is not clear and I followed rest all the steps.The 11nth step I directly used python34 cmd line and typed "import theano".
    I am curious whether am really making use of MinGw here.

    Initially i got the error for "cl.h(or cl.exe i dont remember exasctly) not found".so i added that path to environment variable

    The error I get is "cublas" is not found and at the end "cuda is installed but gpu is not available".

    Kindly advice me where am i going wrong.I am using python34,windows 8.1,64 bit,gpu 920m,visual studio 2013

    ReplyDelete
    Replies
    1. srika,

      The first import of theano must be done with MinGW so it can compile with g++. Make sure to download the MinGW in step 6.2. Link again: http://mingw-w64.yaxm.org/doku.php/download/mingw-builds

      After installing go into the install folder and click mingw-w64.bat. This will open a command prompt, type path and press enter. You should see something like PATH=C:\mingw\\mingw64\bin;... Mingw bin MUST be the first thing in the path variable for theano to compile successfully. Then type python and import theano. If this runs with no errors then you can close the window and import theano from anywhere.

      Let me know if this helps or if I can be of more assistance.

      Delete
    2. I will try again and get back to you.Btw the link you provided for MinGw does not work and the installation fails saying "repository.txt" file is not found

      Delete
    3. Hello IslandMan93,
      Can you explain how you unstalled MinGw64,because the link you gave for MinGw-Builds ,cannot install MinGw.It is asking for a repository.txt file,while installing using the GUI installer.But i have tried using the already existing builds but i cannot find bat file u mentioned at the end.It would be of great help if you could tell us how to install MinGw.I am using 64 bit windows 8.1

      Delete
  2. Hello IslandMan93,


    suddenly MinGw64 got installed.I really do not know why.After following your steps at the 11th step,running "import theano" in mingw command line gives me this error

    "collect2.exe error ld returned 1 exit status"

    Can u tell me which version of MinGw u used,i have installed 5.1.0

    ReplyDelete
    Replies
    1. Srika,

      Glad you got MinGW working. I searched the error and found https://forum.qt.io/topic/35492/collect2-exe-1-error-error-ld-returned-1-exit-status-solved/2

      It may be that you have spaces in your PATH? Or maybe you installed MinGW to a path that has spaces. It's default install location is under C:\Program Files (notice the space).

      Btw I just downloaded and tested MinGW 5.1 and it works so that shouldn't be an issue.

      Delete
  3. Hello IslandMan93,

    I do not have any problem in path due to spaces.I installed mingw in a separate folder like u said.

    When I type path in cmd i get,

    PATH=C:\mingw\mingw64\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.
    0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\libnvvp;;;;C:\Prog
    ram Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Window
    s\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerS
    hell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\P
    rogram Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Fi
    les\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel
    \Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\P
    rogram Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows K
    its\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\T
    ools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\opencv\build
    \x64\vc12\bin;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program F
    iles (x86)\NVIDIA Corporation\PhysX\Common;C:\Python34\Scripts;C:\Program Files
    (x86)\Microsoft Visual Studio 12.0\VC\bin;C:\opencv\build\x64\vc12\lib;C:\Python
    34;C:\Python34\Scripts;


    I have a doubt on what platform am i building for.I installed mingw-w64 with win32 thread and my python 3.4 version is 32 bit.But my system is 64 bit.



    the error i get while importing theano is of the pattern,



    C:/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-
    mingw32/bin/ld.exe: skipping incompatible C:/mingw/mingw64/bin/../lib/gcc/x86_64
    -w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib/libmingw32.a when searchin
    g for -lmingw32


    likewise libgcc_s for -lgcc_s
    libmoldname for -lmoldname


    looks like i am building this for wrong versions.

    Again i am using python3.4.3 32 bit,mingw64 with win32 thread


    ReplyDelete
    Replies
    1. Not sure if this is the problem but can you verify you're using python 32 bit? It should be 64 bit. When you type python in a cmd prompt what does it show?

      I get:
      Microsoft Windows [Version 6.3.9600]
      (c) 2013 Microsoft Corporation. All rights reserved.

      C:\Windows\System32>python
      Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
      Type "help", "copyright", "credits" or "license" for more information.
      >>>

      Note the MSC compiler is 64 bit. If python is using a 32bit compiler then you will definitely get errors trying to use a MinGW 64bit version.

      I would reinstall python. Get the Windows x86-64 MSI installer version from https://www.python.org/downloads/release/python-343/. This is the 64bit version. Or You could try a 32bit version of MinGW (I haven't tested a 32bit mingw though)

      Delete
    2. http://www.islandman93.com/2015/07/tutorial-arcade-learning-environment.html

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. install anaconda,
    run "conda install mingw libpython"
    then "pip install theano"

    http://deeplearning.net/software/theano/install_windows.html

    ReplyDelete
  6. Can I setup/run Theano without a GPU? Can I use my CPU instead?
    What changes would I need to make during the setup process to facilitate this?

    ReplyDelete
  7. Followed steps exactly using Python 3.4.3 on a laptop with Windows 10 and gtx960. Worked perfectly.

    Thank You

    ReplyDelete
  8. Hey,
    I have already installed theano, NVIDIA toolkit, WinPython before. Theano seems to be working in CPU mode and not GPU mode. I have put the .theanorc file as mentioned but still the GPU mode is not on. Also, I get error C1083 saying crtdefs.h couldn't be found. What problem could this be? C++ libraries import errors?
    Regards,
    Ankit

    ReplyDelete
  9. this link is also useful to install these things
    http://xiamenhaim.blogspot.com/2016/02/how-to-install-theano-and-keras-in.html

    ReplyDelete