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.
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.graphattribute. 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.Graphobjects 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
- The following arguments are not supported with the native Keras format 'options
- The meaning of 'Start cannot spawn child process No such file or directory' upon running Tensorflow
- The name tf.Session is deprecated. Please use tf.compat.v1.Session instead
- The print of string constant is always attached with 'b' inTensorFlow
- The Free energy approximation Equation in Restriction Boltzmann Machines
- The input layer disappears from the structure of a deep learning model
- The fastest way execution time to find the longest element in an list
- The following untracked working tree files would be overwritten by merge, but I don''t care

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 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.