Create TensorFlow Summary File Writer For TensorBoard

Use TensorFlow Summary File Writer (tf.summary.FileWriter) to create a TensorFlow Summary Event File for TensorBoard

Use TensorFlow Summary File Writer (tf.summary.FileWriter) to create a TensorFlow Summary Event File for TensorBoard

Video Transcript


In this video, we’re going to use tf.summary.FileWriter to create a TensorFlow Summary FileWriter for TensorBoard.


First, let’s import TensorFlow as tf.

import tensorflow as tf


Then we print out the TensorFlow version that 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, then we’ll look at the graph that’s generated.


First, we define a tf.constant.

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

We give it the value of 10, and we give it the name of the string of “ten”, and we assign it to the Python variable tf_constant_one.


Let’s print the tf_constant_one Python variable to see what we have.

print(tf_constant_one)

We see that it’s a TensorFlow tensor with the name of “ten”, the shape is empty because it’s a scalar, and the data type is int32.


Next, let’s define our second constant scalar.

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

Again, using tf.constant, we give it the value of 20 and the name of the string written out “twenty”.

We assign it to the Python variable tf_constant_two.


Let’s print the tf_constant_two Python variable to see what we have.

print(tf_constant_two)

We see that it’s a TensorFlow tensor, the name is “twenty”, the shape is empty, and the data type is int32.

Remember that TensorFlow builds the graph first and then later evaluates the graph.


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 what you have are uninitialized variables.


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’re going to do tf.add, we pass in our first scalar, tf_constant_one, and we’re going to pass in our second scalar, tf_constant_two, and the result of this will be assigned to the Python variable tf_constant_sum.


Let’s print the tf_constant_sum Python variable to see what we have.

print(tf_constant_sum)

We see that it’s a TensorFlow tensor, we see that it’s an “Add”, we see that the shape is empty signifying that it’s still a scalar, and the data type is int32.


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


Let’s launch the graph in a session.

sess = tf.Session()


Next, let’s initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


All right, so we got here and all of our variables have been initialized.

To run the constant scalar addition in a TensorFlow session, we could just evaluate it with a session run operation.

However, what we want to do is send our TensorFlow graph to TensorBoard so that we can later see what it contains.


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

To do this, we’ll use TensorFlow’s Summary FileWriter.

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

So you can see tf.summary.FileWriter.

We are going to write the file to the “graphs” directory, and what we want to write is the sess.graph.

We assign all of this to the Python variable tf_tensorboard_writer.


What gets written out is called an event file that contains the event protocol buffers.

Note that we pass the session graph so that it will add the TensorFlow graph to the event file.


Now that we have the FileWriter and it’s written the file, let’s doublecheck what our sum actually would have been by running it in a session.

print(sess.run(tf_constant_sum))

We get 30, which is what 10 plus 20 is.

Great.


Now that we’ve done our computation, let’s close the FileWriter.

tf_tensorboard_writer.close()

So we say tf_tensorboard_writer.close().


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

sess.close()


Now that we are done with that, let’s close Python as well so we can navigate to the graph’s folder to see what the TensorFlow Summary FileWriter created in the event file.

exit()


So we’re back on the command line.


We change the directory to the graphs directory

cd graphs

and we do an “ls” to see what is in the directory that’s been generated

ls

and we see our events file.


Let’s take a quick look at this file.

vi events [autocomplete]

So vi events...


So not exactly human readable as the protocol buffers are used for serializing structured data and not necessarily for human reading.


Let’s scroll down the text bits to see what we can find.

We can see Add instructions, we see ten and twenty, and we see a whole bunch of other characters.


Let’s exit.

:q!

And we’re back.


Perfect - It worked!

We were able to use TensorFlow’s summary FileWriter to create a TensorFlow Summary FileWriter for TensorBoard.

Receive the Data Science Weekly Newsletter every Thursday

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