How to install TensorFlow on Windows? - c++

I am starting to work with TensorFlow library for deep learning, https://www.tensorflow.org/.
I found a explicit guide to work on it on linux and Mac but I did not find how to work with it under Windows. I try over the net, but the information are lacking.
I use Visual Studio 2015 for my projects, and I am trying to compile the library with Visual studio Compiler VC14.
How to install it and to use it under Windows?
Can I use Bazel for Windows for production use?

How to install TensorFlow and to use it under Windows?
Updated on 8/4/16
Windows 10 now has a Ubuntu Bash environment, AKA Bash on Ubuntu on Windows, available as a standard option (as opposed to Insider Preview updates for developers). (StackOverflow tag wsl) This option came with the Windows 10 anniversary update (Version 1607) released on 8/2/2016. This allows the use of apt-get to install software packages such as Python and TensorFlow.
Note: Bash on Ubuntu on Windows does not have access to the GPU, so all of the GPU options for installing TensorFlow will not work.
The dated installation instructions for Bash on Ubuntu on Windows are basically correct, but only these steps are necessary:
Prerequisites
Enable the Windows Subsystem for Linux feature (GUI)
Reboot when prompted
Run Bash on Windows
Steps no longer needed:
Turn on Developer Mode
Enable the Windows Subsystem for Linux feature (command-line)
Then install TensorFlow using apt-get
sudo apt-get install python3-pip python3-dev
sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl
and now test TensorFlow
$ python3
...
>>> 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))
42
>>> exit()
and run an actual neural network
python3 -m tensorflow.models.image.mnist.convolutional
Earlier Answer
After learning about the developer preview of Bash on Windows.
See Playing with TensorFlow on Windows by Scott Hanselman which uses Bash on Windows 10
Original Answer
Bazel is the problem
TensorFlow is not made with build automation tools such as make, but with Google's in-house build tool Bazel. Bazel only works on systems based on Unix such as Linux and OS X.
Since the current published/known means to build TensorFlow uses Bazel and Bazel does not work on Windows, one can not install or run TensorFlow natively on Windows.
From Bazel FAQ
What about Windows?
Due to its UNIX heritage, porting Bazel to Windows is significant
work. For example, Bazel uses symlinks extensively, which has varying
levels of support across Windows versions.
We are currently actively working on improving Windows support, but
it's still ways from being usable.
Status
See: TensorFlow issue #17
See: Bazel issue #276
Solutions
The solutions are listed in the order of complexity and work needed; from about an hour to may not even work.
Docker
~ 1 hour
Docker installation
Docker is a system to build self contained versions of a Linux operating system running on your machine. When you install and run TensorFlow via Docker it completely isolates the installation from pre-existing packages on your machine.
Also look at TensorFlow - which Docker image to use?
OS X
~ 1 hour
If you have a current Mac running OS X then see: Installation for Mac OS X
Linux
The recommend Linux system tends to be Ubuntu 14.04 LTS (Download page).
a. Virtual Machine - Hardware Virtualization - Full Virtualization
~ 3 hours
Download and install a virtual machine such as the commercial VMware or the free Virtual Box, after which you can install Linux and then install TensorFlow.
When you go to install TensorFlow you will be using Pip - Python's package management system. Visual Studio users should think NuGet. The packages are known as wheels.
See: Pip Installation
If you need to build from the source then see: Installing From Sources
~ 4 hours
Note: If you plan on using a Virtual Machine and have never done so before, consider using the Docker option instead, since Docker is the Virtual Machine, OS and TensorFlow all packaged together.
b. Dual boot
~ 3 hours
If you want to run TensorFlow on the same machine that you have Windows and make use of the GPU version then you will most likely have to use this option as running on a hosted Virtual Machine, Type 2 hypervisor, will not allow you access to the GPU.
Remote machine
~ 4 hours
If you have remote access to another machine that you can install the Linux OS and TensorFlow software on and allow remote connections to, then you can use your Windows machine to present the remote machine as an application running on Windows.
Cloud Service
I have no experience with this. Please edit answer if you know.
Cloud services such as AWS are being used.
From TensorFlow Features
Want to run the model as a service in the cloud?
Containerize with Docker and TensorFlow just works.
From Docker
Running Docker on AWS provides a highly reliable, low-cost way to
quickly build, ship, and run distributed applications at scale. Deploy
Docker using AMIs from the AWS Marketplace.
Wait for Bazel to work on Windows.
Currently it appears the only hold up is Bazel, however Bazel's roadmap list working on Windows should be available this year.
There are two features listed for Windows:
2016‑02 Bazel can bootstrap itself on Windows without requiring admin privileges.
2016‑12 Full Windows support for Android: Android feature set is identical for Windows and Linux/OS X.
Build TensorFlow by hand.
A few days or more depending on you skill level. I gave up on this one; too many subprojects to build and files to locate.
Remember that Bazel is only used to build TensorFlow. If you get the commands Bazel runs and the correct source code and libraries you should be able to build TensorFlow on Windows. See: How do I get the commands executed by Bazel.
While I have not researched this more, you can look at the continuous integration info for needed files and info on how to they build it for testing. (Readme) (site)
Build Bazel on Windows
A few days or more depending on you skill level. I gave up on this one also; could not find the necessary source files needed for Windows.
There is a public experimental source code version of Bazel that boots on Windows. You may be able to leverage this into getting Bazel to work on Windows, etc.
Also these solutions require the use of Cygwin or MinGW which adds another layer of complexity.
Use alternative build system such as Make
If you get this one to work I would like to see in on GitHub.
This currently does not exist for TensorFlow. It is a feature request.
See: TensorFlow issue 380
Cross Build
If you get this one to work I would like to see in on GitHub.
You build TensorFlow on Linux using Bazel but change the build process to output a wheel that can be installed on Windows. This will require detailed knowledge of Bazel to change the configuration, and locating the source code and libraries that work with Windows. An option I would only suggest as a last resort. It may not even be possible.
Run on the new Windows Subsystem for Linux.
See: Windows Subsystem for Linux Overview
You will know as much as I do by reading the referenced article.
Can I use Bazel for Windows for production use?
Since it is experimental software I would not use on a production machine.
Remember that you only need Bazel to build TensorFlow. So use the experimental code on a non production machine to build the wheel, then install the wheel on a production machine. See: Pip Installation
TLDR;
Currently I have several versions for learning. Most use a VMWare 7.1 Workstation to host Ubuntu 14.04 LTS or Ubuntu 15 or Debian. I also have one dual boot of Ubuntu 14.04 LTS on my Windows machine to access the GPU as the machine with VMware does not have the proper GPU. I would recommend that you give these machines at least 8G of memory either as RAM or RAM and swap space as I have run out of memory a few times.

I can confirm that it works in the Windows Subsystem for Linux!
And it is also very straightforward.
In the Ubuntu Bash on Windows 10, first update the package index:
apt-get update
Then install pip for Python 2:
sudo apt-get install python-pip python-dev
Install tensorflow:
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
The package is now installed an you can run the CNN sample on the MNIST set:
cd /usr/local/lib/python2.7/dist-packages/tensorflow/models/image/mnist
python convolutional.py
I just tested the CPU package for now.
I blogged about it: http://blog.mosthege.net/2016/05/11/running-tensorflow-with-native-linux-binaries-in-the-windows-subsystem-for-linux/
cheers
~michael

Sorry for the excavation, but this question is quite popular, and now it has a different answer.
Google officially announced the addition of Windows (7, 10, and Server 2016) support for TensorFlow:
developers.googleblog.com
The Python module can be installed using pip with a single command:
C:\> pip install tensorflow
And if you need GPU support:
C:\> pip install tensorflow-gpu
TensorFlow manual - How to install pip on windows
Another useful information are included in release notes:
https://github.com/tensorflow/tensorflow/releases
UPD: As #m02ph3u5 right mentioned in the comments TF for windows supports only Python 3.5.x Installing TensorFlow on Windows with native pip

Installing TensorFlow
TensorFlow currently supports only Python 3.5 64-bit. Both CPU and GPU are supported. Here are some installation instructions assuming you do not have Python 3.5 64-bit:
Download and install Microsoft Visual C++ 2015 Redistributable Update 3:
https://www.microsoft.com/en-us/download/details.aspx?id=53587 (required by Python 3.5 and TensorFlow)
Download and install Python 3.5 64-bit: https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe
Install pip as follows: download https://bootstrap.pypa.io/get-pip.py, then run python get-pip.py
Install TensorFlow with either pip install tensorflow (CPU version) or pip install tensorflow-gpu (GPU version --> requires CUDA to be installed).
Testing TensorFlow
You can now run something like following to test whether TensorFlow is working fine:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
a = tf.constant(10)
b = tf.constant(32)
print(sess.run(a + b))
TensorFlow comes with a few models, which are located in C:\Python35\Lib\site-packages\tensorflow\models\ (assuming you installed python in C:\Python35). For example, you can run in the console:
python -m tensorflow.models.image.mnist.convolutional
or
python C:\Python35\Lib\site-packages\tensorflow\models\image\mnist\convolutional.py
Limitations of TensorFlow on Windows
Initial support for building TensorFlow on Microsoft Windows was added on 2016-10-05 in commit 2098b9abcf20d2c9694055bbfd6997bc00b73578:
This PR contains an initial version of support for building TensorFlow
(CPU only) on Windows using CMake. It includes documentation for
building with CMake on Windows, platform-specific code for
implementing core functions on Windows, and CMake rules for building
the C++ example trainer program and a PIP package (Python 3.5 only).
The CMake rules support building TensorFlow with Visual Studio 2015.
Windows support is a work in progress, and we welcome your feedback
and contributions.
For full details of the features currently supported and instructions
for how to build TensorFlow on Windows, please see the file
tensorflow/contrib/cmake/README.md.
The Microsoft Windows support was introduced in TensorFlow in version 0.12 RC0 (release notes):
TensorFlow now builds and runs on Microsoft Windows (tested on Windows 10, Windows 7, and Windows Server 2016). Supported languages include Python (via a pip package) and C++. CUDA 8.0 and cuDNN 5.1 are supported for GPU acceleration. Known limitations include: It is not currently possible to load a custom op library. The GCS and HDFS file systems are not currently supported. The following ops are not currently implemented: DepthwiseConv2dNative, DepthwiseConv2dNativeBackpropFilter, DepthwiseConv2dNativeBackpropInput, Dequantize, Digamma, Erf, Erfc, Igamma, Igammac, Lgamma, Polygamma, QuantizeAndDequantize, QuantizedAvgPool, QuantizedBatchNomWithGlobalNormalization, QuantizedBiasAdd, QuantizedConcat, QuantizedConv2D, QuantizedMatmul, QuantizedMaxPool, QuantizeDownAndShrinkRange, QuantizedRelu, QuantizedRelu6, QuantizedReshape, QuantizeV2, RequantizationRange, and Requantize.

Now Tensorflow is officially supported in Windows, you can install it using pip command of Python 3.5 without compile it yourself
CPU Version
pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0-cp35-cp35m-win_amd64.whl
cp35 indicates python 3.5 wheel, 0.12.0 the version, you can edit these according your preference, or to install latest CPU version available you can use
pip install --upgrade tensorflow
GPU Version
pip install --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-0.12.0-cp35-cp35m-win_amd64.whl
cp35 indicates python 3.5 wheel, 0.12.0 the version, you can edit these according your preference, or to install latest GPU version available you can use
pip install --upgrade tensorflow-gpu
More Info

Following may work for you: install Virtual Box, create Linux VM and install Linux into it. I'd recommend Ubuntu, because Google often uses it internally. Then, install TensorFlow in Linux VM.

You can't at the moment. The problem is that tensorflow uses the bazel build another Google internal tool that has been exposed as an open source project and it has only support for mac and unix.
Until bazel is ported to windows or another build system is added to tensorflow there is a little chance to run tensorflow natively on windows.
That said you can install virtualbox and then install docker-machine and run a linux container with tensorflow inside it.

I managed to install TensorFlow on Win8.1 without Docker using advice from
https://discussions.udacity.com/t/windows-tensorflow-and-visual-studio-2015/45636
I tried a lot of stuff before that, and i won't try to install it twice but here is what i did:
- install VS2015 (make sure Visual C++ installed as well)
- install Python Tools for VS2015
- install Python2.7 with Anaconda2
- install pip and conda for Python
- install numpy with pip inside VS2015
- install tensorflow with pip inside VS2015
i didn't manage to do it with Python3.5
I managed also to install on Win8.1 via Cloud9
There is a video tutorial on Youtube.
https://www.youtube.com/watch?v=kMtrOIPLpR0
EDIT: actually for the above, (not Cloud9 which is fine) i have problems:
TensorFlow LOOKS LIKE it's installed (i can see it in the list of modules installed in VS2015 when clicking in Solution Explorer on Python 64-bit 2.7)
but if i type in a script or in Python Interactive import tensorflow as TF then i get an error message
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\__init__.py", line 23, in <module>
from tensorflow.python import *
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
from tensorflow.python.framework.framework_lib import *
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\python\framework\framework_lib.py", line 62, in <module>
from tensorflow.python.framework.ops import Graph
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\python\framework\ops.py", line 40, in <module>
from tensorflow.python.framework import versions
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\python\framework\versions.py", line 24, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 28, in <module>
_pywrap_tensorflow = swig_import_helper()
File "C:\Users\Fagui\Anaconda2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 20, in swig_import_helper
import _pywrap_tensorflow

As of writing this answer, I wasn't able to get tensorflow to install properly with python version 3.5.2. Reverting to python 3.5.0 did the trick.
Then I was able to install with
C:> pip install tensorflow

If you have already installed anaconda on your windows, there is an easier way as I found out:
conda create --name snakes python=3
Then
activate snakes
Then
pip install tensorflow
This is similar to virtualenv and I found this helpful.

Follow this link to install Tensorflow on Windows and you can also use it in Visual Studio

Related

Installing tensorflow on virtualbox ubuntu 20.04 python 2.7 - 'Illegal instruction (core dumped)'

my goal is to follow this guide so that I can convert a Caffe model to a Tensorflow model. As my original OS is Windows 10 I am using the virtual Ubuntu 20.04 (using Oracle VirtualBox) with python 2.7 and anaconda virtual env. I am able to successfully install tensorflow for python 2.7, but when I run the command python -c 'import tensorflow', I get an error with text Illegal instruction (core dumped). I have tried to google to get more information but all I got was that it maybe has something to do with the architecture, I found out that my architecture (on the virtual ubuntu) is x86_64. That is all I have found out and now I turned to SO to ask what I should do to be able to run tensorflow in the before described environment, many thanks beforehand.
For those wondering, the pip or conda installation did not work in my case, so I followed the instructions on this guide for linux, python 2.7 using the pip installation method with a tensorflow version of 0.10, which for my purposes is good enough.
Exact steps in case the link does not work in the future:
$ conda create -n tensorflow python=2.7
$ conda activate tensorflow
Your prompt should change to this: (tensorflow)$
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl
(tensorflow)$ pip install --ignore-installed --upgrade $TF_BINARY_URL
This is, as outlined, for python 2.7, but the guide contains info for newer versions as well. To check whether tensorflow installed correctly you can run this command $ python -c 'import tensorflow as tf; print(tf.__version__)' which should output 0.10.0.

Cannot install "psycopg2" on Windows 10 with Python 3.8

Yesterday I uninstalled python 3.7 version by mistake. Then I install python again (this time 3.8 version) and again set up my environment. But I could not start my Django project which has Postgres connection. Actually I cannot install "psycopg2" in my environment. I searched for hours and implement every solutions I get from online but it does not work. Let me tell you what I did so far.
First it said to add Postgres in my PATH so I added C:\Program Files\PostgreSQL\12\bin\ in my PATH.
A new problem then arise with a huge ERROR report with 2 vital Error.
ERROR: Failed building wheel for psycopg2
..........................
Running setup.py install for psycopg2 ... error
I try to upgrade wheel but it says,
Requirement already up-to-date
http://initd.org/psycopg/docs/install.html#install-from-source
I learned from this site that psycopg2 requires python2 version. So I installed python 2.7 also.
I reinstalled PostgreSQL but it does not work.
I deleted my virtual environment and create again but it does not work.
Some says they solve this problem by running pip install psycopg2-binary But it does not work for me.
Please help me to get rid of this. I stuck for hours.
When you asked this question, Python 3.8 had been released very recently so there were not any wheels for Python 3.8 yet.
At the time, my suggestion was to install Python 3.7.X and install the binary wheel with:
pip install psycopg2-binary
Since then, binary wheels have been released for Python 3.8, so the above command should work with Python 3.8.X as well.
I wouldn't try to build from source on Windows if it can be avoided.
Finally, you misunderstood the section of the docs about Python 2. You only need Python 2.7 if you are running Python 2. For Python 3, which you should be using for all new projects, it currently supports Python 3.4 to 3.8.
use:
sudo apt install python3-dev libpq-dev
then try doing:
pip3 install psycopg2
Hope it works for you!!
This problem mainly occurs due to this -- " error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/"
Now to install psycopg2, first you need to install visual studio from Microsoft - https://visualstudio.microsoft.com/visual-cpp-build-tools/
now you need to install the c++ desktop development tool with all its default components selected.
After successful visual studio c++ desktop development kit installation, you can now install psycopg2 successfully in your machine.

How to install Xeus-cling without Anaconda?

Why is Anaconda package manager necessary to install Xeus-cling?
I have only used pip so far, and switching to a new package manager seems like a lot of work. I want to start using C++ with Jupyter, but all the installation instructions assume conda package manager.
Does anyone know of Xeus-cling installation which doesn't require Anaconda?
Pip is a package manager for python. It is mostly meant for packaging pure Python packages even though shipping some binary extensions is possible.
xeus-cling is a native application, which relies on a number of other libraries, including LLVM, Clang, Cling, Xeus, OpenSSL, libzmq and other. Vendoring the entire LLVM stack in a pip package would be unreasonable.
The developers of xeus-cling have made the choice of conda because it is a general-purpose cross-platform package manager, but it could be packaged for other general-purpose package managers, such as Debian's dpkg, OSX's Homebrew, conan.io or others.

Howto package a python2.7 project for installation on Centos 6

How do I package a python2.7 project for installation on CentOS 6.7?
I plan on deploying a python2.7 package onto CentOS 6.7 systems that have had python2.7 installed from SCL along side the python2.6 needed for the system (as per this question).
So far, I've tried
using distutils on a CentOS 6.7 machine to make an rpm (bdist_rpm) -- but that's tyring to use python 2.6.
using distutils to make an rpm on a different machine with python2.7 only installed -- the resulting rpm cannot be installed since rpm on the CentOS machine assumes python2.6

Fail to install Python 2.7.9 on a Windows google compute engine instance

I fired up a new Windows google compute engine instance. It's running Windows 2008 R2, service pack 1.
I download and try running the Python .msi installer for version 2.7.9, and it fails with this error:
There is a problem with this Windows Installer package. A program required for this install to complete could not be run. Contact your support personnel or package vendor.
I see this error for both the 64-bit and the 32-bit installer.
Has anyone else seen it or know of a work-around?
I reproduced your issue and I found two workarounds:
You can install python 2.7.6 successfully without further action.
If you need python 2.7.9 you can install it deselecting pip from the install menu.
This seems to be related to this answer in another thread although in that case the issue is with version 3.4.
Install python EXCEPT "pip"
Run the python install msi again and select "change"
Select "pip" and install the pip
It would be works...
I think it is a priority problem into the msi package...the package seems to try to install the pip before installing python.exe. So, pip can not be installed...
I'm using Windows 8.1 64-bit. I had 2.7.11 installed and then I tried to install PIP as well via Chocolatey PIP package.
I think my installation had got messed up because I had tried to install Python 3.4 as well as Python 2.
Then I had deleted all the Python 2 and Python 3 files in an attempt to get rid of this.
What worked for me was:
Editing the Environment Variables both, System and User to remove any PYTHONHOME or PYTHONPATH variables
I also deleted the path I had to python2 in the PATH environment variable
Now (as mentioned in Python Issue 22329) after deleting the Environment variables you can go into 'Programs and Features', click on the Python 2.7.11 (64-bit) program and then click 'Repair' - this then worked as I would expect without error.
Now finally I was able to go into 'Programs and Features', click on the Python 2.7.11 (64-bit) program and then click 'Uninstall'.
Edit: I assume this is connected with this PYTHON_HOME answer to a problem with Python 3.4
It seems to be a dependency issue, please try to install "Microsoft Visual C++ 2008 SP1 Redistributable Package (x64)"