Visualizable Tensorflow

Ignacio Tartavull
Jul 15, 2017 · 2 min read

Visualizing tensorflow graphs can help you understand, debug, and optimize your network. But only if you are able to relate your code to the graph, which is not easy once things get complex.

Image for post
Image for post
The graph produce by the code of the next image
import tensorflow as tfdef create_variables():
a = tf.Variable(0)
b = tf.Variable(0)
return a,b
def do_some_computation(a, b):
sum_of_a_and_b = tf.add(a,b)
some_constant = tf.constant(5)
multiply_sum_by_constant = sum_of_a_and_b * some_constant
return multiply_sum_by_constant
with tf.Session() as sess:
a, b = create_variables()
do_some_computation(a, b)
writer = tf.summary.FileWriter('./summary', sess.graph)

Assigning name to variables, and creating a name scope for every function solves the problem but clutters yours code.

Image for post
Image for post
import tensorflow as tfdef create_variables():
with tf.name_scope('create_variables'):
a = tf.Variable(0, name='a')
b = tf.Variable(0, name='b')
return a,b
def do_some_computation(a, b):
with tf.name_scope('do_some_computation'):
sum_of_a_and_b = tf.add(a,b, name='sum_of_a_and_b')
some_constant = tf.constant(5, name='some_constant')
multiply_sum_by_constant = sum_of_a_and_b * some_constant
return multiply_sum_by_constant
with tf.Session() as sess:
a, b = create_variables()
do_some_computation(a, b)
writer = tf.summary.FileWriter('./ab2', sess.graph)

We can achieve the exact same thing by using the decorators vfun and vclass, defined below in the gist visualizable.py
This decorators are smart enough to find the name of the variable in python and pass it as the name argument to the tensoflow constructor.

import tensorflow as tf@vfun
def create_variables():
a = tf.Variable(0)
b = tf.Variable(0)
return a,b
@vfun
def do_some_computation(a, b):
sum_of_a_and_b = tf.add(a,b)
some_constant = tf.constant(5)
multiply_sum_by_constant = sum_of_a_and_b * some_constant
return multiply_sum_by_constant
with tf.Session() as sess:
a, b = create_variables()
do_some_computation(a, b)
writer = tf.summary.FileWriter('./summary', sess.graph)

And here is the code for visualizable.py
If you find these decorators useful, like this article and I’ll make a proper python package out of them.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store