# Visualizable Tensorflow

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.

`import tensorflow as tfdef create_variables():    a = tf.Variable(0)    b = tf.Variable(0)    return a,bdef 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_constantwith 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.

`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,bdef 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_constantwith 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@vfundef create_variables():    a = tf.Variable(0)    b = tf.Variable(0)    return a,b@vfundef 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_constantwith 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.

Written by

Written by