tf.Summary
TensorFlow
manual creation
machine learning
tutorial

How to manually create a tf.Summary

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

python
1import os
2import tempfile
3import tensorflow as tf
4
5tf.compat.v1.disable_eager_execution()
6
7logdir = os.path.join(tempfile.gettempdir(), "manual-summary-demo")
8writer = tf.compat.v1.summary.FileWriter(logdir)
9
10for step, loss in enumerate([1.2, 0.9, 0.7, 0.55], start=1):
11    summary = tf.compat.v1.Summary(
12        value=[
13            tf.compat.v1.Summary.Value(tag="training/loss", simple_value=loss)
14        ]
15    )
16    writer.add_summary(summary, global_step=step)
17
18writer.flush()
19writer.close()
20
21print(f"Run: tensorboard --logdir {logdir}")

The key pieces are:

  • 'tag is the name shown in TensorBoard'
  • 'simple_value stores a scalar number'
  • 'global_step places 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.

python
1import os
2import tempfile
3import tensorflow as tf
4
5logdir = os.path.join(tempfile.gettempdir(), "tf2-summary-demo")
6writer = tf.summary.create_file_writer(logdir)
7
8values = [0.82, 0.86, 0.89, 0.91]
9
10with writer.as_default():
11    for step, accuracy in enumerate(values, start=1):
12        tf.summary.scalar("validation/accuracy", accuracy, step=step)
13    writer.flush()
14
15print(f"Run: tensorboard --logdir {logdir}")

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:

bash
tensorboard --logdir /tmp/manual-summary-demo

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.Summary is 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_writer and tf.summary.scalar.
  • 'tag, numeric value, and global_step are the core ingredients for useful scalar charts.'
  • If TensorBoard shows nothing, check the log directory and flush the writer.

Course illustration
Course illustration

All Rights Reserved.