7

I would like to simply define a model and visualize its graph in TensorBoard for initial architectural examination. Thus, I would not like to compute anything for this purpose.

In TensorFlow 1.X, it was simple to achieve inside a tf.Session() where I could simply flush() a summary file writer.

In TensorFlow 2.0, there is no tf.Session() and hence the question is how do I achieve it ?

The following is an example code. What additions do I need to make, in order for it to write the graph structure in TensorBoard ?

from nets import i3d
import tensorflow as tf

def i3d_output(model, x):
    out, _ = model(x)
    return out

tf.compat.v1.disable_eager_execution()
x = tf.random.uniform(shape=(4,179,224,224,3))
model = i3d.InceptionI3d()
net = i3d_output(model, x)
train_summary_writer = tf.summary.create_file_writer('/home/uujjwal/bmvc2019')
Ujjwal
  • 1,849
  • 2
  • 17
  • 37

1 Answers1

2

In graph mode use this:

from tensorflow.python.summary.writer.writer import FileWriter
FileWriter('logs/', graph=tf.compat.v1.get_default_graph()).close()

Or this:

tf.compat.v1.summary.FileWriter('log/', graph=tf.compat.v1.get_default_graph()).close()

No need in opening session.

Vlad
  • 8,225
  • 5
  • 33
  • 45
  • `AttributeError: module 'tensorflow' has no attribute 'get_default_graph'` – Ujjwal Mar 30 '19 at 17:06
  • yep, forgot `tf.compat.v1`. Just updated my question – Vlad Mar 30 '19 at 17:07
  • Great this one works. A speculative question but do you think that the inability to do something without using `tf.compat.v1` is a deficiency of the non-eager mode of TF2.0 ? – Ujjwal Mar 30 '19 at 17:09
  • 1
    In TF2 you're supposed to use eager execution and that supposed to be a "good thing". You can do something like `import tensorflow.compat.v1 as tf1` to relieve the pain :-) – Vlad Mar 30 '19 at 17:15
  • 3
    It all looks like a huge crutch. Is there native for TF2.0 way to do this? – Alexander Zot Nov 15 '19 at 04:10
  • An example of TF2.0 graph visualisation without tf.compat.v1; https://stackoverflow.com/a/57582215/2585501 – user2585501 May 03 '22 at 01:30