Building Graphs and Visualizing Using TensorBoard

Know the difference between eager and graph execution, and learn to build computational graphs and visualize them in TensorBoard.

Eager execution

The eager execution mode is the default option for running the code in TensorFlow2.x. In this mode, the operations execute one after another and return the results immediately. In eager execution, tf.matmul(), for instance, multiplies matrices to return the result in a tf.Tensor object without waiting for any other operation to execute.

The eager execution mode simplifies DL model generation. Eager execution greatly reduces our coding effort to build, analyze, and test models. The native Python environment executes TF ops one after another. Therefore, it’s an excellent option to debug and prototype models if we want to save our coding time.

Graph execution

Graph execution, in contrast to eager execution, is the mode followed by TF1.x versions. Coding in graph mode is time-consuming; graph execution is preferred over eager execution for efficiency. This mode takes full advantage of acceleration options because it builds an optimized graph before the evaluation.

Though eager execution is the default mode for TF2.x versions, we can run ops in TF2 using graph execution. If we operate in the graph mode, an operation such as tf.matmul() doesn’t run immediately. TF returns a handle to a graph node and defers the actual multiplication until we explicitly allow it. TF runs optimization to improve execution performance when we defer execution in the graph mode. These optimizations can include combining node operations or removing redundancy.

The TF framework stores our models as computational graphs, regardless of whether we use eager or graph execution. We can save and run graphs without Python code; therefore, models saved as graphs can be deployed in cross-platform environments, such as mobile, web, and embedded applications.

Working in graph mode

In graph execution mode, TF constructs dataflow or computational graphs, which describe the flow or movement of data through a series of processing nodes. Graph nodes represent mathematical operations and tensors form edges between nodes. A series of calculations are performed on tensors at graph nodes using functions when we move forward in the graph. The following figure shows an example of such a graph:

Get hands-on with 1200+ tech skills courses.