How to manually create a tf.Summary
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
Manual summaries are useful when you want TensorBoard output for values that do not come from a built-in TensorFlow training loop. The answer depends on which TensorFlow style you are using, because tf.Summary is a TensorFlow 1.x API while TensorFlow 2 uses tf.summary writers.
Manual Summaries in TensorFlow 1.x Style
If you are maintaining legacy code, you can build a summary object directly with tf.compat.v1.Summary. Then write it into an events file with FileWriter.
The key pieces are:
- '
tagis the name shown in TensorBoard' - '
simple_valuestores a scalar number' - '
global_stepplaces the point on the chart'
This is the direct answer when someone asks how to manually create tf.Summary.
Why Manual Creation Is Useful
Manual summaries help when the metric comes from outside the graph or when you are integrating TensorFlow with another system. Examples include:
- logging validation results computed in plain Python
- writing metrics from a custom simulator
- recording deployment or preprocessing statistics beside training data
Because you control the tag names, you can organize charts clearly. A structure such as training/loss, validation/accuracy, and data/records_per_second makes the TensorBoard dashboard easier to scan.
TensorFlow 2 Equivalent
In TensorFlow 2, the recommended API is not tf.Summary. Instead, create a summary writer and emit scalar data inside its context.
This approach is usually better for new code because it matches eager execution and current TensorFlow tooling.
Choosing Between the Two APIs
Use tf.compat.v1.Summary only when:
- you are maintaining TensorFlow 1.x code
- a library still expects the older summary objects
- you need to interact with legacy graph-based training utilities
Use tf.summary when:
- you are writing new TensorFlow 2 code
- eager execution is enabled
- you want the cleanest path into modern TensorBoard workflows
The output concept is the same in both cases: write event files, then point TensorBoard at the log directory.
Inspecting the Result in TensorBoard
After you run either example, start TensorBoard:
Use the actual directory printed by your script. When TensorBoard starts, open the Scalars page and confirm that your tag names appear. If you wrote multiple runs into separate folders, TensorBoard can compare them side by side.
This inspection step matters because many summary bugs are actually file-path mistakes. Developers often write the data correctly but open TensorBoard against the wrong directory.
Common Pitfalls
The most frequent problem is mixing TensorFlow 1.x and TensorFlow 2 code without realizing it. A script that uses tf.compat.v1.Summary inside a modern eager setup may work awkwardly or confuse future maintainers. Prefer one style consistently.
Another issue is forgetting to call flush() or close(). Event data can stay buffered, which makes it look like TensorBoard is broken even though the process simply has not written everything yet.
People also reuse the same log directory accidentally. That can merge unrelated runs and make charts misleading. Use separate folders for separate experiments when you want clean comparisons.
Summary
- '
tf.compat.v1.Summaryis the manual summary object used by TensorFlow 1.x style code.' - Write manual summaries with
FileWriter.add_summary. - In TensorFlow 2, prefer
tf.summary.create_file_writerandtf.summary.scalar. - '
tag, numeric value, andglobal_stepare the core ingredients for useful scalar charts.' - If TensorBoard shows nothing, check the log directory and flush the writer.
Related reading
- How to map a function with additional parameter using the new Dataset api in TF1.3?
- How to mask vectors in reduce_XXX Tensorflow operations?
- How to merge not all summaries in tensorflow?
- How to merge two saved keras model?
- How to map features from the output of a VectorAssembler back to the column names in Spark ML?
- how to measure the accuracy of knn classifier in python
- How to monitor a filtered version of a metric in EarlyStopping callback in tensorflow?
- How to monitor gradient vanish and explosion in keras with tensorboard?
.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.