Path: blob/master/site/en-snapshot/tensorboard/graphs.ipynb
25115 views
Copyright 2019 The TensorFlow Authors.
Examining the TensorFlow Graph
Overview
TensorBoard’s Graphs dashboard is a powerful tool for examining your TensorFlow model. You can quickly view a conceptual graph of your model’s structure and ensure it matches your intended design. You can also view a op-level graph to understand how TensorFlow understands your program. Examining the op-level graph can give you insight as to how to change your model. For example, you can redesign your model if training is progressing slower than expected.
This tutorial presents a quick overview of how to generate graph diagnostic data and visualize it in TensorBoard’s Graphs dashboard. You’ll define and train a simple Keras Sequential model for the Fashion-MNIST dataset and learn how to log and examine your model graphs. You will also use a tracing API to generate graph data for functions created using the new tf.function
annotation.
Setup
Define a Keras model
In this example, the classifier is a simple four-layer Sequential model.
Download and prepare the training data.
Train the model and log data
Before training, define the Keras TensorBoard callback, specifying the log directory. By passing this callback to Model.fit(), you ensure that graph data is logged for visualization in TensorBoard.
Op-level graph
Start TensorBoard and wait a few seconds for the UI to load. Select the Graphs dashboard by tapping “Graphs” at the top.
You can also optionally use TensorBoard.dev to create a hosted, shareable experiment.
By default, TensorBoard displays the op-level graph. (On the left, you can see the “Default” tag selected.) Note that the graph is inverted; data flows from bottom to top, so it’s upside down compared to the code. However, you can see that the graph closely matches the Keras model definition, with extra edges to other computation nodes.
Graphs are often very large, so you can manipulate the graph visualization:
Scroll to zoom in and out
Drag to pan
Double clicking toggles node expansion (a node can be a container for other nodes)
You can also see metadata by clicking on a node. This allows you to see inputs, outputs, shapes and other details.
Conceptual graph
In addition to the execution graph, TensorBoard also displays a conceptual graph. This is a view of just the Keras model. This may be useful if you’re reusing a saved model and you want to examine or validate its structure.
To see the conceptual graph, select the “keras” tag. For this example, you’ll see a collapsed Sequential node. Double-click the node to see the model’s structure:
Graphs of tf.functions
The examples so far have described graphs of Keras models, where the graphs have been created by defining Keras layers and calling Model.fit().
You may encounter a situation where you need to use the tf.function
annotation to "autograph", i.e., transform, a Python computation function into a high-performance TensorFlow graph. For these situations, you use TensorFlow Summary Trace API to log autographed functions for visualization in TensorBoard.
To use the Summary Trace API:
Define and annotate a function with
tf.function
Use
tf.summary.trace_on()
immediately before your function call site.Add profile information (memory, CPU time) to graph by passing
profiler=True
With a Summary file writer, call
tf.summary.trace_export()
to save the log data
You can then use TensorBoard to see how your function behaves.
You can now see the structure of your function as understood by TensorBoard. Click on the "Profile" radiobutton to see CPU and memory statistics.