Tensorflow - Using tf.summary with 1.2 Estimator API
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The TensorFlow Estimator API provides a high-level interface for training, evaluation, and prediction. Adding tf.summary operations inside the model_fn lets you log metrics, images, histograms, and text to TensorBoard for visualization. In TF 1.x, tf.summary ops are added to the graph and collected automatically when using Estimators — you do not need to create a FileWriter or call sess.run() on summary ops manually. The Estimator handles summary writing during training. This article covers both the TF 1.x Estimator approach and the modern TF 2.x equivalent using Keras callbacks.
TF 1.x: Summaries in model_fn
The Estimator API requires a model_fn that defines the model, loss, optimizer, and any summaries:
Creating and Training the Estimator
The Estimator writes summaries every 100 steps by default. To change this frequency:
Custom Summary Hook
For summaries that need to be computed at specific intervals or with custom logic:
Image Summaries
TF 2.x: Modern Equivalent with Keras
In TensorFlow 2.x, use tf.keras with TensorBoard callback:
Custom Summaries in TF 2.x
Common Pitfalls
- Adding summaries outside
model_fnin TF 1.x: Summary ops must be defined insidemodel_fnwhere the computation graph is built. Defining them outside means they are not part of the Estimator's graph and are never executed. - Expecting summaries during
PREDICTmode: The Estimator only writes summaries duringTRAINmode. Summaries added in the graph are ignored duringPREDICTandEVALmodes unless explicitly included ineval_metric_ops. - Not running TensorBoard with the correct
logdir: Summaries are written to the Estimator'smodel_dir. Runningtensorboard --logdir=wrong_pathshows an empty dashboard. Always matchlogdirto the Estimator'smodel_diror the Keras callback'slog_dir. - Mixing TF 1.x
tf.summarywith TF 2.x code: In TF 2.x,tf.summary.scalar("name", value)requires an activetf.summary.FileWritercontext (with writer.as_default()). The TF 1.x pattern of just callingtf.summary.scalarwithout a writer does not work in TF 2.x eager mode. - Logging too frequently in production: Setting
save_summary_steps=1orupdate_freq="batch"generates massive log files and slows training. Use every 100-500 steps for training andhistogram_freq=1(per epoch) for weight distributions.
Summary
- In TF 1.x Estimators, add
tf.summaryops insidemodel_fn— the Estimator writes them automatically - Configure frequency with
RunConfig(save_summary_steps=N)(default is 100 steps) - Use
tf.summary.scalar,tf.summary.histogram, andtf.summary.imagefor different data types - In TF 2.x, use
tf.keras.callbacks.TensorBoardfor automatic logging ortf.summary.create_file_writerfor custom summaries - View all summaries with
tensorboard --logdir=<model_dir>
Related reading
- Tensorflow - ValueError Failed to convert a NumPy array to a Tensor Unsupported object type float
- Tensorflow - ValueError Failed to convert a NumPy array to a Tensor Unsupported object type float
- Tensorflow - ValueError Parent directory of trained_variables.ckpt doesn''t exist, can''t save
- Tensorflow - ValueError Shape must be rank 1 but is rank 0 for 'ParseExample/ParseExample
- TensorFlow 0.12 tutorials produce warning Rank of input Tensor should be the same as output_rank for column
- Tensorflow 1.0 Windows 64-bit Anaconda 4.3.0 error
- Tensorflow __new__ got an unexpected keyword argument 'serialized_options' in Object Detection API
- Tensorflow ConcatOp Error with Object Detection API

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.