Visualize TensorFlow Graph In TensorBoard

Use TensorFlow Summary File Writer (tf.summary.FileWriter) and the TensorBoard command line utility to visualize a TensorFlow Graph in the TensorBoard web service

Use TensorFlow Summary File Writer (tf.summary.FileWriter) and the TensorBoard command line utility to visualize a TensorFlow Graph in the TensorBoard web service

Video Transcript


In this video, we’re going to use the TensorFlow summary FileWriter, tf.summary.FileWriter and the TensorBoard command line utility to visualize a TensorFlow graph in the TensorBoard web service.


First, let’s import TensorFlow as tf.

import tensorflow as tf


Next, let’s print out what version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.8.0.


The example we’re going to create in this video is to add two named TensorFlow scalars together using the TensorFlow Add operation.


First, let’s define our first constant scalar.

tf_constant_one = tf.constant(10, name="ten")

So we use tf.constant, we give it the value of 10, the name of “ten”, and we assign it to the Python variable tf_constant_one.


Next, let’s define our second constant scalar.

tf_constant_two = tf.constant(20, name="twenty")

So we use tf.constant, we give it a value of 20, the name we give it is the string of “twenty”, and we assign it to the Python variable tf_constant_two.


Since we are in the building the graph stage, both of these TensorFlow constant scalars haven’t been evaluated in a TensorFlow session yet.

So they are uninitialized.


Let’s now build a computational graph node that adds the two constant scalars together.

tf_constant_sum = tf.add(tf_constant_one, tf_constant_two)

So we use tf.add, we pass in tf_constant_one and tf_constant_two, the resulting summation is assigned to the Python variable tf_constant_sum.


Now that we have created our TensorFlow graph, it’s time to run the computational graph.


So we launch the graph in a session

sess = tf.Session()


and we initialize all the global variables in the graph, so our constant one, our constant two, and our constant sum.

sess.run(tf.global_variables_initializer())


Next, we want to send our TensorFlow graph to TensorBoard so that we can visualize the graph.


The way we’ll do this is to use TensorFlow’s summary FileWriter to create a protocol buffer that serializes the structured data so that TensorBoard can later create a visual representation.

tf_tensorboard_writer = tf.summary.FileWriter('./graphs', sess.graph)

So we do tf.summary.FileWriter, we are going to write the event file to the graphs folder, and we’re going to pass in our TensorFlow session graph, and we are assigning the FileWriter to the Python variable tf_tensorboard_writer.


Now that we’re done writing the file, let’s close the FileWriter.

tf_tensorboard_writer.close()


Let’s also close the TensorFlow session to release the TensorFlow resources we used within the session.

sess.close()


Finally, we close Python so we can start TensorBoard from the command line.

exit()


First, let’s check to see if there’s anything in the graph’s directory, so we say ls graphs.

ls graphs

We see that we have our events file that was just written.


Next, let’s use the TensorBoard command line utility to specify where this file lives.

tensorboard --logdir="./graphs"

So we way it lives in the log directory as the graphs.

Now, we can use the TensorBoard command line utility to specify the log directory and have it start the TensorBoard web service for us.


We can see that we’re using TensorBoard 1.8.0.

Rather than using this URL, what we’re going to use is we’re going to use local host.


So I go to the Chrome web browser and go to localhost:6006.

# click in the Chrome web browser

We can see that we are in TensorBoard.

We see that we’re in graphs.

We fit it to the screen and we see that we have an init, a ten, a twenty, and an Add.


We can zoom in and out so we can see a little bit better.


Let’s click on the ten node.

# click in the Chrome web browser

We can see that the operation is a constant, we see that the data type is int32, it’s a tensor, and the init value is 10.


Let’s click on the twenty.

# click in the Chrome web browser

We see that the operation is a constant, we see that the data type is int32, it is a TensorFlow tensor, and the initialized value is 20.


We can then click on the Add operation.

# click in the Chrome web browser


We see that the operation is Add, we see that the data type is int32, and we see our two inputs that are named.

So we have the named scalar input.

We see that it says scalar here, and it’s the name of ten.

We see that our second input is the scalar which has the name of twenty.

So these two inputs are going to be added, and note that we didn’t name the output.

So we don’t have anything here.


We can close this and go back to visualizing our TensorFlow graph.

# click in the Chrome web browser


One thing to note about TensorBoard is that because we’re looking at the graph of the computation, it doesn’t actually show us the result of the addition.

So we don’t actually know what this scalar plus this scalar added equals.

Yes, we know that ten plus twenty equals thirty but we don’t see the actual computation.

We just see the graph.


Perfect - We were able to use the TensorFlow summary FileWriter, tf.summary.FileWriter and the TensorBoard command line utility to visualize a TensorFlow graph in the TensorBoard web service.

Receive the Data Science Weekly Newsletter every Thursday

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Easy to unsubscribe at any time. Your e-mail address is safe.