TensorFlow in Python3

TensorFlow is an Open Source library that Google has released earlier this month. It allows to, in a simple manner, arrange processing and training flows, with elements like neural networks, and even implement new operations over it's ar­chi­tec­ture (tutorial and examples).

This library is written in C++, but the ar­chi­tec­ture, the data to be managed and the operations are declared in Python. This is great, as it yields a great per­for­mance without having to deal with Seg­men­ta­tion Faults, but if you were expecting to use Python3 for this... you may have to wait a while, at this moment it's not supported [tensorflow GitHub issue #1], but it's support is planned.

Meanwhile, in this repo, in the python3 branch, you have a way to use it... it's not completely updated and there's things to tune, like check­points. Also, you'd have to build it manually (there's no prebuild pip package), but we'll see that can be done easily.

Bazel

Well, it turns out that in order to build TensorFlow, it's not enough with make, but it's made to be built with bazel. Install this is pretty easy, the in­struc­tions are in bazel.io. Once the de­pen­den­cies are installed, Bazel has it's own installer, that can be launched piping a curl of their script to a shell (ugh...) or cloning a repo, like the we say says, for this we only have to do this

1
2
3
$ git clone https://github.com/bazelbuild/bazel.git
$ cd bazel
$ ./compile.sh

This will generate a Bazel binary in bazel-bin/src/bazel, that we can move to whatever $PATH we want. We also have a file to help au­to­com­plete in scripts/bazel-complete-template.bash. We can add it to .bashrc with a line like this

1
$ source <PATH TO BAZEL SOURCE>/scripts/bazel-complete-template.bash

Virtual en­vi­ron­ment

Once we have Bazel ready, we have to prepare the virtual en­vi­ron­ment where we'll host the packages which TensorFlow depends upon. For this we only have to

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Create the virtual environment, for example in “tensorflow3-env”
# It doesn't have to be 3.4, another version of python3 may work
$ virtualenv-3.4 --system-site-packages tensorflow3-env
Using base prefix '/usr'
New python executable in tensorflow3-env/bin/python3
Not overwriting existing python script tensorflow3-env/bin/python (you must use tensorflow3-env/bin/python3)
Installing setuptools, pip, wheel...done.
# Activate it
$ source tensorflow3-env/bin/activate

#v-------------v we can seen when we're inside the python virtual environment
(tensorflow3-env)$

# Install TensorFlow dependencies
(tensorflow3-env)$ apt-get install python3-numpy swig python3-dev

TensorFlow

When the en­vi­ron­ment is completely ready, it only remains to generate the pip, package, but wait a moment: it seems that with GCC-5 the process uses too much RAM, so we'll have to avoid para­leliz­ing it so it doesn't clogs the whole PC. So if the GCC version is inferior to 5, the commands would be

1
2
# Prepare the necesary stuff to build the pip package
(tensorflow3-env)$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

In order to (in GCC 5), limit the compiler con­cur­ren­cy, we'll do this:

1
2
# Prepare the necesary stuff to build the pip package
(tensorflow3-env)$ bazel build --ram_utilization_factor=1 -c opt //tensorflow/tools/pip_package:build_pip_package

Once this is done, we can generate the package with

1
(tensorflow3-env)$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

The second argument specifies where the compiled package will be dropped (/tmp/tensorflow_pkg), we can use whichever we want (also, delete it once is installed). After this, it only remains to install the generated package

1
(tensorflow3-env)$ pip install /tmp/tensorflow_pkg/tensorflow-*.whl  # The package name may change

And it's done, we can use the library

1
2
3
4
5
6
(tensorflow3-env)$ python3
Python 3.4.3+ (default, Oct 10 2015, 09:15:38) 
[GCC 5.2.1 20151028] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> # No problem!