Tensorflow - Using tf.summary with 1.2 Estimator API

Viewed 7773

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?

3 Answers

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.

with Tensorflow ver-r1.3

Add your summary ops in your estimator model_fn

example :

tf.summary.histogram(tensorOp.name, tensorOp)

If you feel writing summaries may consume time and space, you can control the writing frequency of summaries, in your Estimator run_config

run_config = tf.contrib.learn.RunConfig()
run_config = run_config.replace(model_dir=FLAGS.model_dir)
run_config = run_config.replace(save_summary_steps=150)

Note: this will affect the overall summary writer frequency for TensorBoard logging, of your estimator (tf.estimator.Estimator)

Related