Monday, April 4, 2016

Tutorial: Theano install on Windows 7, 8, 10

Hello Everyone,

This post is a step by step tutorial on installing Theano for Windows 7, 8, and 10. It uses Theano, CUDA, and Anaconda.

Anaconda is a package manager for python that simplifies setting up python environments and installing dependencies. If you really don't want to use Anaconda, check out my older post here.

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.5)
  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
  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 setup Anaconda
    1. https://www.continuum.io/downloads. The Python 3.5 installer is fine
    2. Install it, it will take awhile ~5-10 minutes
  7. Download Theano
    1. https://github.com/Theano/Theano, Download Zip at the bottom right
    2. Extract
  8. Open CMD prompt
    1. Setup a new conda environment that uses python 3.4
      1. conda create -n name_of_your_environment python=3.4
    2. Activate your conda environment and install dependencies
      1. activate name_of_your_environment
      2. conda install numpy scipy mingw libpython
    3. Navigate to Theano extracted folder /Theano-master
    4. Use python setup.py install
      1. This automatically uses 2to3 conversion
  9. 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\
  10. Final check
    1. Open another CMD prompt (you'll need to close the old one because it doesn't have the system variables)
    2. activate name_of_your_environment
    3. python
    4. import theano
    5. You should see something like
      1. Using gpu device 0: Quadro K1100M (CNMeM is disabled)
Now you'll be able to use Theano when you activate your conda environment.

Note: For pycharm users, PyCharm does not automatically activate the conda environment for you (bug submitted here). What you can do is just create a .bat file with these contents:
    call activate env_name
    path_to_pycharm\bin\pycharm64.exe

Let me know any questions or comments below.

35 comments:

  1. You Legend. Thanks.

    ReplyDelete
  2. Hi there,

    I followed your instructions as closely as I could, but when I try to import theano in the end, I receive the error:

    WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: cuda unavailable)

    Any ideas about how to proceed from here? I thought maybe my gpu has some sort of issue, but so far as I can tell it is operational. Cuda seems like it installed happily as well. So I'm at a loss as to what's going on unfortunately.

    I'd appreciate any help you can offer.
    Thanks

    ReplyDelete
    Replies
    1. Caleb,

      This is most likely a path issue. Make sure you added the Visual Studio bin directory to your path. If you open a cmd prompt you should be able to type cl then press enter and get a response like:
      Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x86
      Copyright (C) Microsoft Corporation. All rights reserved.
      (more stuff here).

      If you get a `cl` is not recognized as an internal or external command. Then cl is not in your path variable. One gotcha is when changing the path variable you must close and reopen the cmd prompt window for the path to be reloaded.

      IslandMan93

      Delete
    2. Your advice worked :D I spent a long time on it before asking, so I really appreciate it. Thanks!

      Delete
    3. Hi,
      I get this error even after updating path variable. upon running "cl" i get following output,

      Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 for x86
      Copyright (C) Microsoft Corporation. All rights reserved.

      usage: cl [ option... ] filename... [ /link linkoption... ]

      Delete
  3. Thanks IslandMan93 you are great.

    ReplyDelete
  4. hello,
    thank you for this.
    I have 2 question please.
    I import theano gives:
    1)''Using gpu device 0: Quadro k610M(cnem is disabled, cudnn not available''
    is it ok to have cuDnn not available?
    2)i have vs2010 and vd2013, but i can only verify my cuda as u have suggested with my vs 2010.. is it ok or ccudnn is not available because of this?

    ReplyDelete
  5. Theano + Windows + Python3 + GPU. Amazing! You are an hero.

    ReplyDelete
  6. Hey, Thanks a lot for this very useful post. I have been trying to use my GPU for like a month. Finally, this worked.

    However, I still have a problem. When I try to import theano in "spyder", that comes with anaconda package, it shows "no module named theano".

    The Anaconda prompt is very inconvenient to use. It would have been great if I could use Spyder to run my Theano code.

    Thanks again.

    ReplyDelete
    Replies
    1. I've never used spyder before, but can offer some suggestions.

      1) Make sure you're running in the environment you created in step 8 by activating it with conda.
      2) You may need to install spyder in this environment with conda install spyder.

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

      Delete
    3. I had the same problem. Check out timctran's post in this thread.

      https://github.com/spyder-ide/spyder/issues/2858

      However it still didn't work for me so I've resorted to using IDLE.

      Delete
  7. Thank you again for posting this. One day, I hope a windows theano install can be this easy.

    Unfortunately, I followed the above closely, and got a dll load error. I could be wrong, but I don't think it's BLAS related, as it occurs even with ldflags=
    "" set. anyways, the problem is here:

    File "c:\learning\theano_stuff\theano-master\theano\gof\cmodule.py"
    , line 302, in dlimport
    rval = __import__(module_name, {}, {}, [module_name])
    ImportError: DLL load failed: The specified procedure could not be found.

    Any suggestions on tracking this one down? Nothing is built in the theano cache, so I can't use the dependency walker (or don't know what I'd use it on).

    Thanks again

    ReplyDelete
    Replies
    1. Nevermind, I figured it out. Sort of. If anyone has a similar problem, reset your path variable to the bare necessities. I had some old references from an earlier install of theano that were causing problems.

      With that said, install was as easy as above. I was even able to make a conda environment that uses the GPU. Thanks again for the guide, islandman.

      Delete
    2. Nevermind, I figured it out. Sort of. If anyone has a similar problem, reset your path variable to the bare necessities. I had some old references from an earlier install of theano that were causing problems.

      With that said, install was as easy as above. I was even able to make a conda environment that uses the GPU. Thanks again for the guide, islandman.

      Delete
  8. Thank you for the effort of sharing this.
    Is it supposed to work on 64 bit as well ?

    ReplyDelete
    Replies
    1. What is your opinion on on Anaconda missing free Intel MKL support (they are in the MKL Optimizations and Accelerate add-ons for Anaconda - I'm not sure if they use PLASMA and MAGMA either). With MKL optimization, numpy has outperformed IDL on large matrix computations by 10-fold.

      Delete
  9. Thank you for your post. Very helpful! I still encounter however the following error while going through the "final check":
    "d:\anaconda2\envs\midasik\include\pyconfig.h(224) : fatal error C1083: Cannot open include file: 'basetsd.h': No such file or directory
    mod.cu"

    Do you see any solution for that? (I already installed SDK, so no idea what's more i should do...)

    ReplyDelete
  10. Thank you:)... it works !!!

    ReplyDelete
  11. I owe you a great debt of gratitude for this terrific post... thanks!

    ReplyDelete
  12. Can anybody please tell me. If someone want to create a neural scheme without using coding method instead using a chains of commands significant of ways how schematics works in nature of instructions. Is there any software which can help me to build an Artificial Intelligence like the genuine one. I am not a programmer neither i can use programming software's to use or create some kind of program. It just i know what to programme but don't know how to programme need a simple way to execute set of commands into my system to activate a thinking process. I know guys its a big thing but there is a way to create the thing in 6-7 months which scientist suggested as it will be achieve in a decade or 6,7 years later...

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. Hiii... I have tried following your steps and everything went fine upto step-9. But in step-10 while activating my environment it gave an error 'activate' is not recognized.
    I even tried out the suggestion mentioned in the reply of caleb Moses but still it didn't work. Please help me

    ReplyDelete
    Replies
    1. It's most likely the anaconda installation did not get correctly added to your path variable. Make sure during the installation you mark the checkbox that says something like add anaconda (or conda) to the system PATH variable. A simple test would be to reinstall Anaconda and open a CMD prompt and type activate and see if the command is recognized. If this step fails, the rest won't work. Relevant github issue is https://github.com/conda/conda/issues/1519.

      Delete
  16. Hi, I'm not able to install mingw libpython. It says it belongs to python V. 2.6. I'm using python 3.6.

    If you please help.

    ReplyDelete
  17. Traceback (most recent call last):
    File "C:\Users\Uzel\Desktop\DEEP LEARNÝNG\calisanlenet.py", line 13, in
    import theano
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\__init__.py", line 42, in
    from theano.configdefaults import config
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\configdefaults.py", line 226, in
    in_c_key=False)
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\configparser.py", line 266, in AddConfigVar
    root=newroot, in_c_key=in_c_key)
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\configparser.py", line 276, in AddConfigVar
    configparam.__get__(root, type(root), delete_key=True)
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\configparser.py", line 324, in __get__
    self.__set__(cls, val_str)
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\configparser.py", line 335, in __set__
    self.val = self.filter(val)
    File "C:\Users\Uzel\Anaconda2\lib\site-packages\theano\configparser.py", line 383, in filter
    % (val, self.fullname), val)
    ValueError: ('Invalid value (True;C:Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin) for configuration variable "nvcc.fastmath".', 'True;C:Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin')


    I do all things but I have a this error. What do you think about that, What should I do?

    ReplyDelete
    Replies
    1. It looks like you accidentally copied the path to Visual Studios bin folder to the nvcc.fastmath flag.
      The Theano flags should end with:
      nvcc.fastmath=True
      Then in step 9.3 add that string to your PATH variable not the Theano flags variable.

      Delete
    2. thank you so much I solved I think. But another problem I have now. My cuda install but dont work



      5367 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=79 :
      5368
      ===============================
      nvcc fatal : nvcc cannot find a supported version of Microsoft Visual Studio. Only the versions 2010, 2012, and 2013 are supported

      ['nvcc', '-shared', '-O3', '-LC:\\Users\\Uzel\\Anaconda2\\libs', '-use_fast_math', '--compiler-bindir', 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin', '-Xlinker', '/DEBUG', '-D HAVE_ROUND', '-m64', '-Xcompiler', '-DCUDA_NDARRAY_CUH=c72d035fdf91890f3b36710688069b2e,-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,/Zi,/MD', '-IC:\\Users\\Uzel\\Anaconda2\\lib\\site-packages\\theano\\sandbox\\cuda', '-IC:\\Users\\Uzel\\Anaconda2\\lib\\site-packages\\numpy\\core\\include', '-IC:\\Users\\Uzel\\Anaconda2\\include', '-IC:\\Users\\Uzel\\Anaconda2\\lib\\site-packages\\theano\\gof', '-o', 'C:\\Users\\Uzel\\AppData\\Local\\Theano\\compiledir_Windows-8.1-6.3.9600-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.13-64\\cuda_ndarray\\cuda_ndarray.pyd', 'mod.cu', '-LC:\\Users\\Uzel\\Anaconda2\\libs', '-LC:\\Users\\Uzel\\Anaconda2', '-lcublas', '-lpython27', '-lcudart']
      ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: ('nvcc return status', 1, 'for cmd', 'nvcc -shared -O3 -LC:\\Users\\Uzel\\Anaconda2\\libs -use_fast_math --compiler-bindir C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin -Xlinker /DEBUG -D HAVE_ROUND -m64 -Xcompiler -DCUDA_NDARRAY_CUH=c72d035fdf91890f3b36710688069b2e,-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,/Zi,/MD -IC:\\Users\\Uzel\\Anaconda2\\lib\\site-packages\\theano\\sandbox\\cuda -IC:\\Users\\Uzel\\Anaconda2\\lib\\site-packages\\numpy\\core\\include -IC:\\Users\\Uzel\\Anaconda2\\include -IC:\\Users\\Uzel\\Anaconda2\\lib\\site-packages\\theano\\gof -o C:\\Users\\Uzel\\AppData\\Local\\Theano\\compiledir_Windows-8.1-6.3.9600-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.13-64\\cuda_ndarray\\cuda_ndarray.pyd mod.cu -LC:\\Users\\Uzel\\Anaconda2\\libs -LC:\\Users\\Uzel\\Anaconda2 -lcublas -lpython27 -lcudart')
      WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: cuda unavailable)
      Downloading data from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz

      Delete
    3. [global]
      floatX = float32
      device = gpu

      [nvcc]
      flags=-LC:\Users\Uzel\Anaconda2\libs
      compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin

      And here is my .theanorc text file

      Delete
    4. Looks like you're using Visual Studio 2015, you'll have to instead use Visual Studio 2013. Looks like you might have to sign up for a microsoft account. The information is on this page https://www.visualstudio.com/vs/older-downloads/

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

      Delete
  18. cuda is installed gpu is working now. Sorry for that but I have another problem maybe you can help me one more time :) mnist.pkl file can not directory or file I don't know.


    DEBUG: nvcc STDOUT mod.cu
    Creating library C:/Users/Uzel/AppData/Local/Theano/compiledir_Windows-8.1-6.3.9600-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.13-64/tmpdyw_g6/265abc51f7c376c224983485238ff1a5.lib and object C:/Users/Uzel/AppData/Local/Theano/compiledir_Windows-8.1-6.3.9600-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-2.7.13-64/tmpdyw_g6/265abc51f7c376c224983485238ff1a5.exp

    Using gpu device 0: GeForce GT 740M (CNMeM is enabled with initial size: 80.0% of memory, cuDNN 5005)

    Downloading data from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz

    Traceback (most recent call last):
    File "C:\Users\Uzel\Desktop\4.sýnýf 2.dönem\DEEP LEARNÝNG\calisanlenet.py", line 394, in
    evaluate_lenet5()
    File "C:\Users\Uzel\Desktop\4.sýnýf 2.dönem\DEEP LEARNÝNG\calisanlenet.py", line 180, in evaluate_lenet5
    datasets2 = load_data(dataset)
    File ".\logistic_sgd.py", line 205, in load_data
    urllib.request.urlretrieve(origin, dataset)
    File "C:\Users\Uzel\Anaconda2\lib\urllib.py", line 98, in urlretrieve
    return opener.retrieve(url, filename, reporthook, data)
    File "C:\Users\Uzel\Anaconda2\lib\urllib.py", line 249, in retrieve
    tfp = open(filename, 'wb')

    IOError: [Errno 2] No such file or directory: 'C:\\Us\\user\\Documents\\CALISMALAR\\neural-networks-and-deep-learning-master\\neural-networks-and-deep-learning-master\\data\\mnist.pkl.gz'

    ReplyDelete