Thursday, February 2, 2017

Setting up TensorFlow on Windows

TensorFlow, since it has been released just over an year ago, has gained a huge popularity with its capabilities. But the platforms it worked on was limited - there were only pre-packaged Python packages for Linux initially, while it gained a Mac OS compatible version along the way. Windows users were left wondering when (or if) there will be a Windows version of TensorFlow. There has been few unofficial builds for Windows, and some have attempted to build from source for Windows with various degrees of success. But, there was nothing official.

Now, I'm happy to learn that TensorFlow now officially supports Windows. The release notes suggests that Windows compatibility was present from TensorFlow v0.12.0 RC0, and it has been tested on Windows 7, Windows 10, and Windows Server 2016. It only supports 64-Bit Python 3.5. Both the standard Python distribution (from Python.org) and Anaconda Python are supported.

TensorFlow Logo


This was great news for me, since I was switching between Linux and Windows for my machine learning experiments. So, I gave a try to setup TensorFlow on Windows.

I've been using Anaconda Python for most of my experiments, so I created an Anaconda environment - named 'tensorflow' - with all the basic packages I need.
 conda create --name tensorflow numpy scipy scikit-learn pillow h5py mingw libpython  

Then I activated the environment I just created,
 activate tensorflow  

Now for the big step, installing TensorFlow from pip.


I chose the CPU only version for testing. (I will test out the GPU version later)

 pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl  

Created the environment, and running the TensorFlow install command
Created the environment, and running the TensorFlow install command


The installation went ahead. But it seems TensorFlow overwrites some of the packages installed by conda.

Installation overwriting some conda packages
Installation overwriting some conda packages

The install completed, but I got a FileNotFoundError for setuptools-27.2.0-py3.5.egg.


The FileNotFoundError at the end of the installation
The FileNotFoundError at the end of the installation


After the install, when I checked the list of installed packages using conda list, it showed that numpy and setuptools packages has been duplicated - one installed by conda and another by pip.


Packages duplicated after the installation
Packages duplicated after the installation

However, when I tested the TensorFlow installation, using the instructions from the official setup guide, the test commands ran successfully.

 python
 >>> import tensorflow as tf  
 >>> hello = tf.constant('Hello, TensorFlow!')  
 >>> sess = tf.Session()  
 >>> print(sess.run(hello))  
 Hello, TensorFlow!  
 >>> a = tf.constant(10)  
 >>> b = tf.constant(32)  
 >>> print(sess.run(a + b))  

Testing the TensorFlow installation
Testing the TensorFlow installation


I still wasn't satisfied with the installation - since it has overwritten the conda packages, and it gave an error at the end of the install.  So, I decided to remove the conda environment, and try again.

 deactivate  
   
 conda remove --name tensorflow --all  
   
 conda create --name tensorflow numpy scipy scikit-learn pillow h5py mingw libpython  

 activate tensorflow

This time, I used the following 2 commands to install only the missing dependencies, and leave the already installed packages intact.

 pip install --upgrade --no-deps https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl  
   
 pip install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl  

Re-installing TensorFlow while keeping the dependencies intact
Re-installing TensorFlow while keeping the dependencies intact


Tested the installation again, and it ran successfully again,

Re-testing the installation, successfully
Re-testing the installation, successfully

Finally, I ran the MNIST example in TensorFlow, and it too ran without errors.

 python -m tensorflow.models.image.mnist.convolutional  

Running the MNIST example on TensorFlow on Windows
Running the MNIST example on TensorFlow on Windows

Now, I'm ready to experiment with TensorFlow on Windows.


Summary

In order to properly install TensorFlow on Windows on Anaconda Python, use the following steps.
  1. Create and activate an Anaconda environment, with the following libraries,
     conda create --name tensorflow numpy scipy scikit-learn pillow h5py mingw libpython  
       
     activate tensorflow  
    

  2. Install Tensorflow from pip with only the missing dependencies (not upgrading already installed packages),
     pip install --upgrade --no-deps https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl   
         
     pip install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl  
    

  3. Test the installation,
     python  
     >>> import tensorflow as tf  
     >>> hello = tf.constant('Hello, TensorFlow!')  
     >>> sess = tf.Session()  
     >>> print(sess.run(hello))  
     Hello, TensorFlow!  
     >>> a = tf.constant(10)  
     >>> b = tf.constant(32)  
     >>> print(sess.run(a + b))  
    

  4. Run an example model, e.g. MNIST
     python -m tensorflow.models.image.mnist.convolutional  
    


I'll test out the TensorFlow GPU version on Windows next.

Update 10/Mar/17: I've added a new post on Setting up TensorFlow with CUDA on Windows.

Related Links:


2 comments:

  1. perfect, written just right in time as I now tried to install tf on win10. Solves all the installer problems. Thank you!

    ReplyDelete