Printing extra training metrics with Tensorflow Estimator
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
TensorFlow's Estimator API logs loss by default during training, but you often need to monitor additional metrics like accuracy, learning rate, or custom values. The Estimator framework provides two mechanisms for this: tf.estimator.LoggingTensorHook to print tensor values during training, and tf.summary operations combined with TensorBoard for visualization. The key is adding the metrics to the EstimatorSpec and configuring hooks to display them.
Adding Metrics to the Model Function
The model function returns an EstimatorSpec that defines what to compute during training. Add extra metrics by creating tensors and passing them to a logging hook:
LoggingTensorHook Options
Referencing Tensors by Name
If you cannot pass the tensor object directly, use the tensor's string name:
This is useful when the hook is created outside the model function.
Adding TensorBoard Summaries
For persistent metric tracking, add summary operations:
Custom SessionRunHook
For more control, create a custom hook:
Setting Log Level
By default, TensorFlow only shows warnings. To see logging hook output:
Without setting the verbosity to INFO, LoggingTensorHook output is suppressed.
Complete Example
Common Pitfalls
- Forgetting
tf.logging.set_verbosity(tf.logging.INFO): Without this,LoggingTensorHookoutput is invisible. The hook runs but prints nothing because TF defaults to WARNING level. - Using
accuracy[0]instead ofaccuracy[1]:tf.metrics.accuracyreturns(value, update_op). Use index[1](the update op) for training hooks because the value tensor is not updated during training without running the update op. - Passing hooks via
train()instead ofEstimatorSpec: Both work, but hooks inEstimatorSpec.training_hooksare model-specific, while hooks inestimator.train(hooks=[...])are session-level. Usetraining_hooksfor metrics tied to the model. - TF 2.x deprecation: The Estimator API is deprecated in TF 2.x. Use
tf.keraswith callbacks (tf.keras.callbacks.TensorBoard, customCallbacksubclasses) for new projects. - Summary ops not appearing in TensorBoard: Summaries must be created inside the model function. Summaries created outside the Estimator's graph are not captured. Also ensure
model_dirpoints to the correct directory.
Summary
- Use
tf.estimator.LoggingTensorHookto print extra metrics during training - Pass tensors as a dict to the hook and set
every_n_iterorevery_n_secs - Include the hook in
EstimatorSpec(training_hooks=[hook]) - Set
tf.logging.set_verbosity(tf.logging.INFO)or output will be suppressed - Use
tf.summary.scalarfor TensorBoard visualization - For TF 2.x, prefer Keras callbacks instead of the Estimator API
Related reading
- Problems with real-valued input deep belief networks of RBMs
- Process output data from YOLOv5 TFlite
- Processing time gets longer and longer after each iteration TensorFlow
- Proper way to feed time-series data to stateful LSTM?
- Printing the loss during TensorFlow training
- Problem with running object_detection_tutorial TypeError load missing 2 required positional arguments
- Probability and Neural Networks
- Probability prediction method of KNeighborsClassifier returns only 0 and 1
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.