Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

I have a TensorFlow model, and one part of this model evaluates the accuracy. The accuracy is just another node in the tensorflow graph, that takes in logits and labels.

When I want to plot the training accuracy, this is simple: I have something like:

tf.scalar_summary("Training Accuracy", accuracy)

tf.scalar_summary("SomethingElse", foo)

summary_op = tf.merge_all_summaries()

writer = tf.train.SummaryWriter('/me/mydir/', graph=sess.graph)

Then, during my training loop, I have something like:

for n in xrange(1000):

  ...

  summary, ..., ... = sess.run([summary_op, ..., ...], feed_dict)

  writer.add_summary(summary, n)

  ...

Also inside that for loop, every says, 100 iterations, I want to evaluate the validation accuracy. I have a separate feed_dict for this, and I am able to evaluate the validation accuracy very nicely in python.

However, here is my problem: I want to make another summary for the validation accuracy, by using the accuracy node. I am not clear on how to do this though. Since I have the accuracy node it makes sense that I should be able to re-use it, but I am unsure how to do this exactly, such that I can also get the validation accuracy written out as a separate scalar_summary...

How might this be possible?

1 Answer

0 votes
by (33.1k points)

You can use summery writers to solve your problem. You can also reuse the accuracy node but you need to use two different SummaryWriters, one for the training accuracy and another for the test data. You should also assign the scalar summary for accuracy to a variable.

For example:

accuracy_summary = tf.scalar_summary("Training Accuracy", accuracy)

tf.scalar_summary("SomethingElse", foo)

summary_op = tf.merge_all_summaries()

summaries_dir = '/me/mydir/'

train_writer = tf.train.SummaryWriter(summaries_dir + '/train', sess.graph)

test_writer = tf.train.SummaryWriter(summaries_dir + '/test')

During the training phase, you should also record the training accuracy with train_writer. Then run the graph on the test set, each 100th iteration and record only the accuracy summary with the test_writer.

# Record train set summaries, and train

summary, _ = sess.run([summary_op, train_step], feed_dict=...)

train_writer.add_summary(summary, n)

if n % 100 == 0:  # Record summaries and test-set accuracy

  summary, acc = sess.run([accuracy_summary, accuracy], feed_dict=...)

  test_writer.add_summary(summary, n)

  print('Accuracy at step %s: %s' % (n, acc))

Simply, point TensorBoard to the parent directory (summaries_dir) and it will load both data sets.

Hope this answer helps.

If you wish to know more about  TensorFlow visit this TensorFlow Tutorial.

Browse Categories

...