TensorFlow
sess.graph
tf.get_default_graph
Python
machine learning

The difference between sess.graph and tf.get_default_graph?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In TensorFlow 1.x, managing computational graphs is a fundamental concept. TensorFlow uses graphs to represent computations as data flow graphs, where nodes represent operations, and edges represent data, or tensors, flowing between operations. Understanding the difference between sess.graph and tf.get_default_graph() is critical to effectively utilizing TensorFlow 1.x. Below, we'll delve into these components with technical explanations, examples, and subtopics.


Computational Graphs in TensorFlow 1.x

In TensorFlow, a computational graph is a series of TensorFlow operations arranged into a graph of nodes. Each graph contains a set of operations, and each operation is a node in the graph.

Understanding sess.graph

When you create a tf.Session , it is automatically associated with a computational graph. This graph is accessible via sess.graph . Here’s how it works:

  • Graph association: Every session in TensorFlow 1.x is associated with exactly one graph, and this is the graph that is executed when running a session. This graph contains all operations and tensors for the session.
  • Accessing session's graph: You can access this graph using the sess.graph attribute. It is important to note that this graph is read-only and any modifications need to be done before the session runs.

Example of sess.graph

  • Default graph: TensorFlow automatically provides a default graph where all operations are added if no other graph context is specified.
  • Creating new operations: When you create operations without explicitly defining a graph, they are added to the default graph, which can be fetched with tf.get_default_graph() .
  • Explicit Graph Creation: Creating explicit tf.Graph objects is useful for managing multiple computational graphs. This is especially handy when creating modular code without interfering operations.
  • Setting as Default: You can set a custom graph as the default using with graph.as_default(): , which temporarily overrides the default graph scope.
  • Eager Execution: In TensorFlow 2.x, eager execution is enabled by default, meaning operations are evaluated immediately. This reduces the need to manage computational graphs explicitly, unlike TensorFlow 1.x.
  • Migrating Code: Understanding legacy graph-based execution is still crucial for transitioning older code to TensorFlow 2.x or maintaining older systems.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track 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.

Practice ML system design

All Rights Reserved.