Save Tensorflow graph for viewing in Tensorboard w

2019-02-08 15:25发布

问题:

I have a rather complicated Tensorflow graph that I'd like to visualize for optimization purposes. Is there a function that I can call that will simply save the graph for viewing in Tensorboard without needing to annotate variables?

I Tried this:

merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/Users/Name/Desktop/tf_logs", session.graph_def)

But no output was produced. This is using the 0.6 wheel.

This appears to be related: Graph visualisaton is not showing in tensorboard for seq2seq model

回答1:

For efficiency, the tf.train.SummaryWriter logs asynchronously to disk. To ensure that the graph appears in the log, you must call close() or flush() on the writer before the program exits.



回答2:

You can also dump the graph as a GraphDef protobuf and load that directly in TensorBoard. You can do this without starting a session or running the model.

## ... create graph ...
>>> graph_def = tf.get_default_graph().as_graph_def()
>>> graphpb_txt = str(graph_def)
>>> with open('graphpb.txt', 'w') as f: f.write(graphpb_txt)

This will output a file that looks something like this, depending on the specifics of your model.

node {
  name: "W"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
...
version 1

In TensorBoard you can then use the "Upload" button to load it from disk.



回答3:

This worked for me:

graph = tf.Graph()
with graph.as_default():
    ... build graph (without annotations) ...
writer = tf.summary.FileWriter(logdir='logdir', graph=graph)
writer.flush()

The graph is loaded automatically when launching tensorboard with "--logdir=logdir/". No "upload" button needed.



回答4:

For all clarity, this is how I used the .flush() method and resolved the issue:

Initialize the writer with:

writer = tf.train.SummaryWriter("/home/rob/Dropbox/ConvNets/tf/log_tb", sess.graph_def)

and use the writer with:

writer.add_summary(summary_str, i)
    writer.flush()