Scanners and diagrams of tensor maps are duplicated

I use TensorBoard to visualize network metrics and graphs.

I create a sess = tf.InteractiveSession() session and create a graph in a Jupyter laptop.

In the graph, I include two summary scalars:

 with tf.variable_scope('summary') as scope: loss_summary = tf.summary.scalar('Loss', cross_entropy) train_accuracy_summary = tf.summary.scalar('Train_accuracy', accuracy) 

Then create summary_writer = tf.summary.FileWriter(logdir, sess.graph) and run:

_,loss_sum,train_accuracy_sum=sess.run([...],feed_dict=feed_dict)

I write metrics:

 summary_writer.add_summary(loss_sum, i) summary_writer.add_summary(train_accuracy_sum, i) 

I run the code three times.

Every time I start, I re-import the TF and create a new interactive session.

But in Tensorboard, a separate scalar window is created for each run:

enter image description here

In addition, the graph will be duplicated if I check the data for the last run:

enter image description here

How to prevent duplicate graph and scalar window every time I run?

  • I want all data to be displayed on the same scalar graphs (with multiple episodes / plot).
  • I want each run to refer to the visualization of one graph.
+5
source share
2 answers

I suspect that the problem arises because you run the code three times in the process (same script, Jupyter laptop or something else), and these calls have the same default schedule in TensorFlow. TensorFlow must give each node in the graph a unique name, so it adds "_1" and "_2" to the names of the resulting nodes in the second and third calls.

How do you avoid this? The easiest way is to create a new chart every time you run the code. There are (at least) three ways to do this:

  • Wrap the code in a block with tf.Graph().as_default(): which creates a new tf.Graph and sets it by default for the size of the with block.

  • If you create a session before creating a graph, you can create a session as sess = tf.InteractiveSession(graph=tf.Graph()) . The newly constructed tf.Graph object remains as the default chart until you call sess.close() .

  • Call tf.reset_default_graph() between code calls.

The with -block approach is the โ€œmost structuredโ€ way to do something, and it might be better if you are writing a stand-alone script. However, since you are using tf.InteractiveSession , I assume that you are using some kind of interactive REPL, and the other two approaches are probably more useful (for example, to split the execution into several cells).

+5
source

This problem occurs for storing multiple graphs, this is not a problem if you want to solve this problem:

tf.reset_default_graph ()

0
source

Source: https://habr.com/ru/post/1265584/


All Articles