Search code examples
loggingtensorflowtensorboard

How do I prevent summary (or scope) "proliferation" migrating from tf.scalar_summary to tf.summary.scalar?


Back before 2016-11-30 I could use TensorFlow/TensorBoard code something like the following to create a single scope containing a variable 'global_step_at_epoch' that showed the global step reached at each epoch of my model runs.

But since replacing scalar_summary with summary.scalar, as below, I get a new scope for each epoch. So that after n epochs have completed, I get scopes, and TensorBoard panels, for 'global_step_at_epoch', 'global_step_at_epoch_1', 'global_step_at_epoch_2', ... 'global_step_at_epoch_n', each with a single point.

How do I migrate from scalar_summary to summary.scalar so that the code below (or something like it) produces consolidates all of the individual scopes into a single one, as scalar_summary used to do?

global_step = tf.Variable(0, trainable=False, name='global_step')
test_writer = tf.summary.FileWriter(...)

with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                      log_device_placement=FLAGS.log_device_placement)) as sess:
    sess.run(tf.initialize_all_variables())

    test_writer.add_summary(sess.run(tf.summary.scalar('global_step_at_epoch', 0)), 0)

    for ep in range(epochs):

        mini_batches = ...

        for mini_batch in mini_batches:

            gs = int(sess.run(global_step))

        test_writer.add_summary(sess.run(tf.summary.scalar('global_step_at_epoch', gs)), ep + 1)

Solution

  • This can already be accomplished with the tf.name_scope context manager by naming the manager the first time it is used and simply suing it again when you wish to add new items to the existing scope (without "proliferation"):

    with tf.name_scope('foo') as a_scope:
       # some graph elements A
    
    # Elsewhere ...
    
    with tf.name_scope(a_scope):
      # additional graph elements B
    

    This will place the A and B elements together in a single scope 'foo', whereas

    with tf.name_scope('foo') as:
       # some graph elements A
    
    with tf.name_scope('foo'):
      # additional graph elements B
    

    will place the elements in separate scopes, foo and foo_1, respectively.