Saturday, February 24, 2018

Scientific Python Environment on Windows with Tensorflow

Google has made Tensorflow quite easy to install with GPU support, but getting it working along side SciPy and Numpy in a single Windows environment can be tricky. This is usually because Scipy looks for a specific build of Numpy that uses Intel MKL. This guide will allow you to set it up easily.

Firstly, I setup WinPython on my machine. You get Scikit packages, Jupyter, Scipy, Numpy, Spyder etc. in simple easy distribution that doesn't have to be installed but kept side-by-side with multiple setups. Avoid the "Zero" builds that have minimal packages. I used Python 3.5 Qt 5 installer.

Second, go to the Tensorflow website and determine the CUDA version you need and then install CUDA of that version. If you install the incorrect version, you'll keep getting the error:
ImportError: Could not find 'cudart64_90.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 9.0 from this URL: https://developer.nvidia.com/cuda-toolkit
Here, I installed CUDA 9.1 when instead the Python Package Index version of Tensorflow (v1.5 at the time of writing) required 9.0. Afterwards, install the cuDNN that matches the CUDA you have installed. This requires a sign up before login at the time of writing. Older versions of Tensorflow did not require cuDNN installed, but this is now compulsory.

Third, install tensorflow-gpu via the WinPython Command Prompt by issuing the usual command that Google recommends:
pip install --upgrade --ignore-installed tensorflow-gpu
This will give you a working Tensorflow, Numpy environment, but Scipy will be broken giving the error:
    from scipy.linalg import _fblas

ImportError: DLL load failed: The specified module could not be found.
This is where you will need to install the Numpy MKL version, which you can find here. Then use the WinPython Control Panel to upgrade the Numpy package. Make sure to download the Numpy MKL wheel to match your Python distribution.

EDIT: If you choose the version of WinPython released just after the Tensorflow release you desire, then most of the libraries will be in-sync with the Tensorflow PIP package and therefore won't give you the issues with Scipy (and other libraries) above.

That's it!

Cheers Shakes - L3mming