Using summary with tf slim or tf layers
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 summaries (tf.summary) let you log scalars, histograms, images, and other data to TensorBoard for visualization during training. When using tf.slim or tf.layers, summaries are not automatically collected — you must explicitly add them or configure the layers to emit them. In TF 1.x, summaries are graph operations merged with tf.summary.merge_all() and written by a tf.summary.FileWriter. In TF 2.x, tf.summary uses eager-mode writers and works differently.
Adding Summaries with tf.layers (TF 1.x)
tf.layers does not create summary operations by default. You add them manually after defining layers:
Adding Summaries with tf.slim
tf.slim provides slim.summarize_tensors() and slim.summarize_collection() to batch-add summaries:
slim.summarize_tensors() adds histogram summaries for each tensor in the list. slim.summarize_activation() adds both a histogram and a scalar summary (for sparsity) of an activation tensor.
Using slim.learning.train() with Summaries
slim.learning.train() handles summary writing automatically:
Custom Summaries for Loss and Metrics
TF 2.x Approach (Keras and Eager Mode)
In TF 2.x, tf.slim and tf.layers are deprecated in favor of tf.keras.layers. Summaries use a different API:
Or use the built-in TensorBoard callback with model.fit():
Common Pitfalls
- Forgetting
tf.summary.merge_all(): In TF 1.x, individualtf.summary.*calls create operations but do not run them. You must merge them and evaluate the merged op insess.run(). Without this, no summaries are written to disk. - Adding summaries after
merge_all():merge_all()only merges summaries that exist at the time it is called. Summaries added afterward are not included. Define all summaries before callingmerge_all(). - Summary name collisions: Two summaries with the same name overwrite each other in TensorBoard. Use unique names or scopes (
with tf.name_scope('train'):) to namespace your summaries. - Writing summaries every step: Writing summaries on every training step slows training significantly because histogram computation and disk I/O are expensive. Write every N steps or use
save_summaries_secsto throttle. - Mixing TF 1.x and TF 2.x summary APIs:
tf.compat.v1.summary(TF 1.x) andtf.summary(TF 2.x) use different writers and are not interchangeable. Pick one API and use it consistently.
Summary
tf.layersrequires manualtf.summary.histogram()calls for weights and activationstf.slimprovidessummarize_tensors()andsummarize_activation()helpersslim.learning.train()handles summary merging and writing automatically- In TF 1.x, always call
tf.summary.merge_all()and run the merged op in the session - In TF 2.x, use
tf.summary.create_file_writer()with eager mode or the TensorBoard Keras callback - Write summaries periodically (not every step) to avoid slowing down training
Related reading
- Using Syntaxnet with TensorFlow Serving
- Using Tensorboard to monitor training real time and visualize the model architecture
- Using Tensorflow 2.0 and eager execution without Keras
- Using tensorflow dataset with stratified sampling
- Using Tensorflow Huber loss in Keras
- Using Tensorflow Layers in Keras
- Using tensorflow on Android NDK side directly Not using JAVA api
- Using TensorFlow through Jupyter Python 3
.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.