TensorFlow Data Flow Graph Optimization

TensorFlow represents an user program as a computation graph/data flow graph where each node represents a mathematical computation eg. add, subtract, matrix multiply, ReLU, etc and each edge represents input/output tensor data. A node has zero or more input edges and zero or more output edges. Data flow graph is an important design because TensorFlow can perform the following code optimizations using the knowledge about the computation graph.

  • Remove dead nodes. These are the source nodes, sink nodes, control flow nodes, and stateful nodes.
  • Remove identity nodes. (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/common_runtime/function.cc)
  • Perform constant folding to check if a node can be evaluated as constant and therefore eligible for subsequent constant propagation.
  • Perform function inlining.
  • Perform common subexpression elimination. This technique is to find the common subexpressions within the graph and replacing them with a single computation to avoid redundant computation Read more about common subexpression elimination here

Check out the full implementation in graph_optimizer.cc

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/common_runtime/graph_optimizer.cc

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/common_runtime/constant_folding.cc

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/graph/optimizer_cse.cc

TensorFlow Papers

Great papers on TensorFlow

A Tour of TensorFlow

TensorFlow: A System for Large-Scale Machine Learning

TensorFlow: Large-Scale Machine Learning on Heterogeneous Distributed Systems

A Comparison of Distributed Machine Learning Platforms

TensorFlow Estimators: Managing Simplicity vs. Flexibility in
High-Level Machine Learning Frameworks

The TensorFlow Partitioning and Scheduling Problem: It’s the Critical Path!

Run Tensorflow on Mac using Docker

Here are the steps to run Tensorflow on Mac using Docker

1) Install docker
https://docs.docker.com/docker-for-mac/install

2)Launch docker container with Tensorflow CPU binary image and launch a Jupyter notebook

docker run -it -v /MY_LOCAL_COMPUTER_DIR:/MAPPED_DIR_ON_DOCKER -p 8888:8888 -p 6006:6006 tensorflow/tensorflow

Note: -v is to map your specified local directory to a directory on Docker. In this way, you can make your specified local directory accessible in your Docker instance. In my example command below, I specified this local directory on my computer /Users/weishungchung/aiformankind to be made available on Docker instance as /aiformankind.

docker run -it -v /Users/weishungchung/aiformankind:/aiformankind -p 8888:8888 -p 6006:6006 tensorflow/tensorflow

Screen Shot 2018-05-24 at 7.12.10 PM

3) Copy the Jupyter notebook URL printed on the console (replace localhost with 192.168.99.100, this is docker-machine default ip)

http://192.168.99.100:8888/?token=7b91b5a355727bcf3ae1da7135a96e68bca40ccad673bf21

4) Log into Jupyter using the given token

Screen Shot 2018-05-24 at 7.14.53 PM
5) Open the hello_tensorflow.ipynb notebook and run it

Screen Shot 2018-05-24 at 7.19.50 PM

6) Open and run the 3_mnist_from_scratch.ipynb

Screen Shot 2018-05-24 at 7.23.47 PM

7) Install Keras

Log into docker container

Open another terminal, we want to log into container to install Keras.

On the terminal, run the command below

eval "$(docker-machine env default)"

Find your container id by running docker ps
You can see your container id in the console printout. In this example, mine is 2b89e5ae9430

docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                            NAMES
2b89e5ae9430        tensorflow/tensorflow   "/run_jupyter.sh --a…"   41 minutes ago      Up 41 minutes       0.0.0.0:6006->6006/tcp, 0.0.0.0:8888->8888/tcp   dazzling_villani

Then log into container by running the following command
(In this example, 2b89e5ae9430 is the container id)

docker exec -it 2b89e5ae9430  bash

Screen Shot 2018-05-24 at 8.01.00 PM

8) Once you are in the container, you are ready to install Keras

pip install keras

Screen Shot 2018-05-24 at 8.08.53 PM

9) Verify Keras version by running the following python command

python -c 'import keras; print(keras.__version__)'

Screen Shot 2018-05-24 at 8.17.11 PM

Deep Learning on AWS GPU Instance

Amazon has released a Deep Learning AMI and makes the process of running deep learning on GPU way easier than before. Before the availability of this AMI, I had to go through the painstaking process of installing all the required CUDA and cuDNN libraries and then spending lots of time in debugging just to get everything running. This AMI makes the on-boarding process much much easier and smoother. Great works indeed !

Follow the steps in the following blog to launch the instance.
https://aws.amazon.com/blogs/machine-learning/get-started-with-deep-learning-using-the-aws-deep-learning-ami/

Go to AWS Marketplace, search for deep learning AMI (ubuntu) and create an instance from the image

Screen Shot 2018-03-31 at 9.58.13 AM

Select the p3.2xlarge instance type. Make sure you check the hourly pricing for selected instance. It adds up quickly for GPU instance. For p3.2xlarge instance, it costs ~ $3.06/hour. See this EC2 pricing link.

Note: Remember to terminate your instance after use.

Screen Shot 2018-04-08 at 2.08.53 PM

Review and Launch your instance

Screen Shot 2018-04-08 at 2.11.38 PM

Screen Shot 2018-03-31 at 10.02.21 AM

SSH to your new instance

ssh -L localhost:8888:localhost:8888 -i ~/.ssh/my-key-pair.pem ubuntu@xx.xxx.xxx.xxx


Screen Shot 2018-04-08 at 3.08.48 PM

Activate TensorFlow + Keras 2 on Python 3 with CUDA 8 using the following command

source activate tensorflow_p36


Run some examples
cd ~/tutorials/TensorFlow/board

python mnist_with_summaries.py
Screen Shot 2018-03-31 at 10.43.36 AM.png

Start tensorboard using the following command. You can then access the tensorboard UI at http://your-aws-instance-public-ip:6006
tensorboard --logdir=/tmp/tensorflow/mnist
Screen Shot 2018-04-08 at 3.22.42 PM

Screen Shot 2018-03-31 at 12.18.52 PM

Try out other examples

git clone https://github.com/keras-team/keras.git

cd keras/examples

python cifar10_cnn.py

Screen Shot 2018-04-08 at 4.56.48 PM