Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Machine Learning by (4.2k points)

Is there a way to let Tensorflow print extra training metrics (e.g. batch accuracy) when using the Estimator API?

One can add summaries and view the result in Tensorboard (see another post), but I was wondering if there is an elegant way to get the scalar summary values printed while training. This already happens for training loss, e.g.:

loss = 0.672677, step = 2901 (52.995 sec)

but it would be nice to have e.g.

loss = 0.672677, accuracy = 0.54678, step = 2901 (52.995 sec)

without to much trouble. I am aware that most of the time it is more useful to plot test set accuracy (I am already doing this with a validation monitor), but in this case I am also interested in training batch accuracy.

1 Answer

+2 votes
by (6.8k points)

From what I've read it is not possible to change it by passing parameter. You can try to do by creating a logging hook and passing it into to estimator run.

In the body of model_fn operate for your estimator:

logging_hook = tf.train.LoggingTensorHook({"loss" : loss, "accuracy" : accuracy}, every_n_iter=10)

# Rest of the function 

return tf.estimator.EstimatorSpec(

...params...

training_hooks = [logging_hook])

You can conjointly use the TensorBoard to check some graphics of the required metrics. To do that, add the metric to a TensorFlow outline like this:

accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])

tf.summary.scalar('accuracy', accuracy[1])

The cool thing when you use the tf.estimator.The estimator is that you don't need to add the summaries to a FileWriter since it's done automatically (merging and saving them every 100 steps by default).

Don't forget to change this line as well, based on the accuracy parameter you just added:

eval_metric_ops = { "accuracy": accuracy }

return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

In order to check the TensorBoard you need to open a replacement terminal and type:

tensorboard --logdir={$MODEL_DIR}

After that, you will be able to see the graphics in your browser at localhost:6006.

A lot of questions regarding this will be cleared through the Tensorflow Tutorial

Browse Categories

...