Search code examples
tensorflowtensorboard

how to merge my training and validation accuracy in one graph using tensorboard


The tensorboard shows multiple graphs for training and validation accuracy for each step and I want it to show the changes in both accuracy on a single graph.

def accuracy(predictions, labels):
 return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
      / predictions.shape[0])

num_steps = 20000
with tf.Session(graph = graph) as session:
  tf.global_variables_initializer().run()
  print(loss.eval())    
  summary_op = tf.summary.merge_all()
  summaries_dir = '/loggg/'
  train_writer = tf.summary.FileWriter(summaries_dir, graph)
  for step in range(num_steps):
    _,l, predictions = session.run([optimizer, loss, predict_train])

    if (step % 2000 == 0):
          #print(predictions[3:6])                
          print('Loss at step %d: %f' % (step, l))
          training = accuracy( predictions, y_train[:, :])
          validation = accuracy(predict_valid.eval(), y_test)
          print('Training accuracy: %.1f%%' % training)
          print('Validation accuracy: %.1f%%' % validation)
          accuracy_summary = tf.summary.scalar("Training_Accuracy", training)
          validation_summary = tf.summary.scalar("Validation_Accuracy", validation)                                        
          Result = session.run(summary_op)
          train_writer.add_summary(Result, step)
          train_writer.close()

result Image of tensorboard showing multiple training and validation accuracy on different graphs


Solution

  • Each call to tf.summary.scalar() defines a new op in the graph, so since you're calling it within your train loop, each iteration generates a different summary op which each have a different _1, _2, etc. suffix, and this results in many different plots within TensorBoard.

    If you're just getting started, I'd recommend trying out the Keras API or using eager execution, which both make it easier to avoid this problem.

    If you need to use the graph + session model explicitly, then the entire graph should be constructed up-front, including the accuracy computation (converted to TensorFlow ops, not numpy), the tf.summary.scalar() calls to record the accuracies, and finally the tf.summary.merge_all() op. Then within the training loop you would only do sess.run(), writer.add_summary(), and writer.flush().