Search code examples
tensorflowtensorboard

Plot separate learning curves with tensorboard


myplot: accuracy

Running a NN model with tensorflow, I want to plot the accuracy score on both training set and test set. However, the plot tensorboard showed me looked weird: there was only one 'accuracy' tab there, and it plotted the two scores on that same figure. So basically, every step on the x axis has two points connected together. How can I plot two lines (training accuracy and test accuracy) separately on that figure?

Here's a snippet of my code:

loss_summary = tf.summary.scalar('loss', loss) acc_summary = tf.summary.scalar('accuracy', accuracy)

summary_loss, summary_acc_train = sess.run([loss_summary, acc_summary], feed_dict={X: X_train, y: y_train}) summary_acc_test = sess.run([acc_summary], feed_dict={X: X_test, y: y_test})

summary_writer.add_summary(summary_loss, epoch) summary_writer.add_summary(summary_acc_train, epoch) summary_writer.add_summary(summary_acc_test, epoch)


Solution

  • You need to create two different summary writers:

    train_summary_writer = tf.summary.FileWriter(os.path.join(SUMMARIES_DIR, "train"), sess.graph)
    validation_summary_writer = tf.summary.FileWriter(os.path.join(SUMMARIES_DIR, "validation"), sess.graph)
    
     ...
    
    train_summary_writer.add_summary(summary_loss, epoch)
    train_summary_writer.add_summary(summary_acc_train, epoch)
    validation_summary_writer.add_summary(summary_acc_test, epoch)