Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow - Using tf.summary with 1.2 Estimator API

I'm trying to add some TensorBoard logging to a model which uses the new tf.estimator API.

I have a hook set up like so:

summary_hook = tf.train.SummarySaverHook(
    save_secs=2,
    output_dir=MODEL_DIR,
    summary_op=tf.summary.merge_all())

# ...

classifier.train(
    input_fn,
    steps=1000,
    hooks=[summary_hook])

In my model_fn, I am also creating a summary -

def model_fn(features, labels, mode):
    # ... model stuff, calculate the value of loss
    tf.summary.scalar("loss", loss)
    # ...

However, when I run this code, I get the following error from the summary_hook: Exactly one of scaffold or summary_op must be provided. This is probably because tf.summary.merge_all() is not finding any summaries and is returning None, despite the tf.summary.scalar I declared in the model_fn.

Any ideas why this wouldn't be working?

like image 496
Daniel Avatar asked Jul 13 '17 16:07

Daniel


People also ask

What is Estimator API in TensorFlow?

Estimators simplify sharing implementations between model developers. You can develop a great model with high-level intuitive code, as they usually are easier to use if you need to create models compared to the low-level TensorFlow APIs. Estimators are themselves built on tf. keras.

What kind of estimator model does TensorFlow recommend using for classification?

It is recommended using pre-made Estimators when just getting started. To write a TensorFlow program based on pre-made Estimators, you must perform the following tasks: Create one or more input functions. Define the model's feature columns.

What is tf summary?

The tf. summary module provides APIs for writing summary data. This data can be visualized in TensorBoard, the visualization toolkit that comes with TensorFlow. See the TensorBoard website for more detailed tutorials about how to use these APIs, or some quick examples below.

What are the benefits of using the estimator API?

What r the benefits of using estimator API ? You can train both locally and in a distributed model training environment. It provides a high-level API, simplifying model and development. It automatically saves summaries to TensorBoard.


2 Answers

Use tf.train.Scaffold() and pass tf.merge_all as following

summary_hook = tf.train.SummarySaverHook(
    save_secs=2,
    output_dir=MODEL_DIR,
    scaffold=tf.train.Scaffold(summary_op=tf.summary.merge_all()))
like image 116
kdkyum Avatar answered Oct 04 '22 02:10

kdkyum


Just for whoever have this question in the future, the selected solution doesn't work for me (see my comments in the selected solution).

Actually, with TF 1.2 Estimator API, one doesn't need to have summary_hook. I just have tf.summary.scalar("loss", loss) in the model_fn, and run the code without summary_hook. The loss is recorded and shown in the tensorboard. I'm not sure if TF API was changed after this and similar questions.

like image 25
EXP0 Avatar answered Oct 04 '22 03:10

EXP0