Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image of graph in TensorFlow?

Tags:

I wrote a simple script to calculate the golden ratio from 1,2,5. Is there a way to actually produce a visual through tensorflow (possibly with the aid of matplotlib or networkx) of the actual graph structure? The doc of tensorflow is pretty similar to a factor graph so I was wondering:

How can an image of the graph structure be generated through tensorflow?

In this example below, it would be C_1, C_2, C_3 as individual nodes, and then C_1 would have the tf.sqrt operation followed by the operation that brings them together. Maybe the graph structure (nodes,edges) can be imported into networkx? I see that the tensor objects have a graph attribute but I haven't found out how to actually use this for imaging purposes.

#!/usr/bin/python  import tensorflow as tf C_1 = tf.constant(5.0) C_2 = tf.constant(1.0) C_3 = tf.constant(2.0)  golden_ratio = (tf.sqrt(C_1) + C_2)/C_3  sess = tf.Session() print sess.run(golden_ratio) #1.61803 sess.close() 
like image 794
O.rka Avatar asked Dec 11 '15 18:12

O.rka


People also ask

How do you show a TensorFlow graph?

Select the Graphs dashboard by tapping “Graphs” at the top. You can also optionally use TensorBoard. dev to create a hosted, shareable experiment. By default, TensorBoard displays the op-level graph.

What tool helps visualize a TensorFlow graph?

TensorBoard is a powerful visualization tool built straight into TensorFlow that allows you to find insights in your ML model. TensorBoard can visualize anything from scalars (e.g., loss/accuracy), to images, histograms, to the TensorFlow graph, to much more.


2 Answers

This is exactly what tensorboard was created for. You need to slightly modify your code to store the information about your graph.

import tensorflow as tf C_1 = tf.constant(5.0) C_2 = tf.constant(1.0) C_3 = tf.constant(2.0)  golden_ratio = (tf.sqrt(C_1) + C_2)/C_3  with tf.Session() as sess:     writer = tf.summary.FileWriter('logs', sess.graph)     print sess.run(golden_ratio)     writer.close() 

This will create a logs folder with event files in your working directory. After this you should run tensorboard from your command line tensorboard --logdir="logs" and navigate to the url it gives you (http://127.0.0.1:6006). In your browser go to GRAPHS tab and enjoy your graph.

You will use TB a lot if you are going to do anything with TF. So it makes sense to learn about it more from official tutorials and from this video.

like image 196
Salvador Dali Avatar answered Sep 21 '22 05:09

Salvador Dali


You can get an image of the graph using Tensorboard. You need to edit your code to output the graph, and then you can launch tensorboard and see it. See, in particular, TensorBoard: Graph Visualization. You create a SummaryWriter and include the sess.graph_def in it. The graph def will be output to the log directory.

like image 40
dga Avatar answered Sep 20 '22 05:09

dga