Tensorflow Graphs are just protobufs.

Ouwen Huang
2 min readJul 25, 2018

--

Jupyter Notebook Here, (First post here).

In a Tensorflow computational graph, nodes are tf.Operation objects and edges pass tf.Tensor objects. A tf.Operation then takes tf.Tensor objects as inputs and outputs.

Figure 1: The figure above is an example of a computational graph. Each node {A, B, C, D, E, F} performs some computational operation. {A} will square inputs, {B} will cube inputs, {C} adds one, etc. Each individual node is dependent only on its inputs. The program is created by the way nodes are wired together.

Let’s create the computational graph (figure 1) using the mathematical tf.Operation objects provided here. Important note: we are only building the graph, not evaluating. This is equivalent to typing numbers into the calculator without pressing enter.

Each of the operations above can take in a tensor-like object (scalar, list, numpy array). tf.Tensor should feel very similar to python numpy.

However, tf.Tensor objects are only symbolic handles, and do not actually provide a concrete value until evaluated. It is a redeemable prize ticket, not the prize itself.

We will use tensorboard, a graph visualization tool to view our built graph.

Every node (including the input constant) is a tf.Operation, and every line passes a tf.Tensor.

We have now defined a graph which can be saved in a protocol buffer text format. Protocol buffers are just like any other structure notation such as JSON, XML, HTML. Protobuf is a preferred choice because they are very compact, and are strongly typed. Tensorflow graphs use the protobuf GraphDef to define a graph. This GraphDef protobuf can be exported and imported.

We can view graph g1 by running g1.as_graph_def() this will output the text form of the graph, which we view below.

This protobuf contains the numpy matrix input graph, the full ascii video can be viewed here. The simple float32 gist in this article can be found here.

This protobuf file contains everything needed to reconstruct a tensorflow graph. You can load in the graph_protobuf.pbtxt to retrieve the program. Changing the internals of this file is analogous to programming a new graph program.

So far we have only been admiring our graph program, but we have not actually run it yet. We will do so with the tensorflow Session API here.

--

--