Working with multiple graphs in TensorFlow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Working with multiple graphs in TensorFlow mostly matters in TensorFlow 1.x style code, where operations belong to an explicit tf.Graph and sessions run against a chosen graph. In TensorFlow 2, eager execution makes explicit graph management much less common, though graph concepts still exist inside tf.function. So the first step is to know which TensorFlow programming model you are in.
Multiple Graphs in TensorFlow 1.x
In TensorFlow 1.x, every operation belongs to a graph. If you do nothing special, TensorFlow uses the default graph. When you want isolation between models, separate variable namespaces, or independent session lifecycles, you can create multiple graphs explicitly.
The constants and operations in graph1 do not belong to graph2, and vice versa.
Sessions Must Match the Graph
To execute operations, create sessions bound to the correct graph.
This separation is useful when you want two independent computational setups in the same Python process.
A common use case in older codebases was loading two different models without mixing their tensors and variable names accidentally.
Why Multiple Graphs Were Useful
In TensorFlow 1.x, multiple graphs helped with:
- isolating separate models
- avoiding default-graph clutter in notebooks or long-running processes
- loading different saved graphs independently
- testing graph-building logic without name collisions
For example, a server process might load one graph for image classification and another for text classification, each with its own session.
Be Careful With Cross-Graph Mixing
A tensor from one graph cannot be used directly inside another graph's operations.
This kind of mistake is invalid:
Each op's inputs must belong to the same graph.
That rule is one of the reasons explicit graph management could become confusing in large TensorFlow 1.x programs.
TensorFlow 2 Changes the Picture
TensorFlow 2 uses eager execution by default, which means operations run immediately like normal Python code. You usually do not create multiple tf.Graph() objects in day-to-day TF2 code.
Instead, you typically work with:
- eager tensors
- Keras models
- '
tf.functionfor graph tracing where performance matters'
A simple TF2 example:
No explicit graph object is needed here.
When Graph Thinking Still Matters in TF2
Even in TensorFlow 2, graphs have not disappeared. tf.function traces Python functions into graph-like computation for optimization.
But this is not the same experience as manually juggling multiple tf.Graph() instances and sessions. For most modern code, the framework handles that graph construction for you.
Practical Guidance
If you are maintaining TensorFlow 1.x code, explicit multiple graphs can still be useful and sometimes necessary.
If you are writing new TensorFlow code, prefer TensorFlow 2 patterns unless you have a specific legacy integration reason not to.
That usually means:
- use Keras models
- avoid manual session management
- use
tf.functionselectively for performance - treat explicit multiple graphs as a legacy or niche tool
Common Pitfalls
The most common mistake in TensorFlow 1.x is creating operations in the wrong default graph and then trying to run them in another session.
Another mistake is mixing tensors from different graphs. TensorFlow will reject those combinations.
Developers also often carry TensorFlow 1.x graph-management habits into TensorFlow 2 even when eager execution makes them unnecessary.
Finally, in notebooks, forgetting to reset or isolate graph construction can lead to duplicate names and confusing state. Multiple explicit graphs were sometimes a workaround for exactly that issue.
Summary
- Multiple explicit graphs are mainly a TensorFlow 1.x concept.
- Use
tf.Graph()pluswith graph.as_default()to isolate graph construction. - Sessions must run operations from the graph they are bound to.
- Tensors and operations cannot be mixed across graphs.
- In TensorFlow 2, eager execution and
tf.functionreduce the need for manual graph management.
Related reading
- Xavier and he_normal initialization difference
- Y label shape for time_distributed lstm
- YOLO object detection how does the algorithm predict bounding boxes larger than a grid cell?
- 4D input in LSTM layer in Keras
- Working with SSIM loss function in tensorflow for RGB images
- Write tf.dataset back to TFRecord
- wrong model type for regression error in 10 fold cross validation for Naive Bayes using R
- XGBModel' object has no attribute 'evals_result_

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.