Saturday, December 5, 2015

Tensorflow jupyter notebook

In an orbit near you

At the last PYPTUG meeting, I demoed Google's Tensorflow deep learning toolkit. I did that using the Jupyter notebook. If you are not familiar with this, check out and you'll be  able to play with Python, R, Ruby, Scala, Bash etc.

To install jupyter on your computer, pip3 is your friend (more detail at

pip3 install jupyter
By installing jupyter, you'll also get the ipython kernel, so you'll be able to create new jupyter notebooks for python. There are over 50 programming languages supported by jupyter. But that is not all. You can also create specific environments and associate notebooks with them. It works great on pretty much any platform, including the Raspberry Pi, Windows, the Mac, Linux etc. Each kernel has a varying degree of availability, and the same can be said of python modules. Tensorflow will not run on the Pi at this time...

Tensorflow notebook

New notebook dropdown in Jupyter 4
What we are going to do here is to install Tensorflow in a virtual environment, and create a notebook configuration so we can have the choice in the new-> dropdown, as pictured above.

If you've tried to install Tensorflow, particularly on a Mac,  you have probably found it challenging, as Tensorflow requires Python 2.7.


On a Mac, since Apple's Python 2.7 is kind of messed up...
brew install python
Create a virtualenv and activate it, then install requirements:
virtualenv -p /usr/local/bin/python2.7 tensor
source tensor/bin/activate

pip install numpy scipy sklearn pandas matplotlib
pip install

Configure Jupyter

I have a globally available Jupyter notebook, on Python 3. This allows me to run various kernels from one notebook. Even virtualenvs, as I'll show here. The below is for Mac. On a Linux or Windows machine it'll be similar: use jupyter --paths to find the data directory (the kernels folder will be in there).
(tensor)LTOXFDION:~ francois$ pip install ipython ipykernel
LTOXFDION:kernels francois$ pwd
LTOXFDION:kernels francois$ ls
ir python2
LTOXFDION:kernels francois$ cp -r python2/ tensor
LTOXFDION:kernels francois$ cd tensor/
LTOXFDION:tensor francois$ vi kernel.json

In the editor you'll have to modify the path to python to point to your directory. If you dont have a python2 directory to copy, just create a tensor directory and create the kernel.json file. In the end, your kernel.json file should look something like:

 "display_name": "TensorFlow (Py2)",
 "language": "python",
 "argv": [
  "-c", "from ipykernel.kernelapp import main; main()",
  "-f", "{connection_file}"
 "codemirror_mode": {
  "version": 2,
  "name": "python"

You should be good to go now. Start jupyter:

  jupyter notebook

You'll be able to create a new notebook for Tensorflow. From there, all you have to do is import it:

    import tensorflow as tf

We'll continue on this next time.

Francois Dion

1 comment:

@danbri said...

For TF on Pi, see