TensorFlow
machine learning
graph management
deep learning
computational graphs

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.

Practice ML system design

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.

python
1import tensorflow.compat.v1 as tf
2
3tf.disable_eager_execution()
4
5graph1 = tf.Graph()
6graph2 = tf.Graph()
7
8with graph1.as_default():
9    x1 = tf.constant(2.0)
10    y1 = tf.constant(3.0)
11    sum_op = x1 + y1
12
13with graph2.as_default():
14    x2 = tf.constant(10.0)
15    y2 = tf.constant(4.0)
16    product_op = x2 * y2

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.

python
1with tf.Session(graph=graph1) as sess1:
2    print(sess1.run(sum_op))
3
4with tf.Session(graph=graph2) as sess2:
5    print(sess2.run(product_op))

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:

python
1with graph1.as_default():
2    a = tf.constant(1.0)
3
4with graph2.as_default():
5    b = tf.constant(2.0)
6    # c = a + b  # invalid: a belongs to graph1

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.function for graph tracing where performance matters'

A simple TF2 example:

python
1import tensorflow as tf
2
3x = tf.constant(2.0)
4y = tf.constant(3.0)
5print(x + y)

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.

python
1import tensorflow as tf
2
3@tf.function
4def add(a, b):
5    return a + b
6
7print(add(tf.constant(2.0), tf.constant(3.0)))

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.function selectively 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() plus with 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.function reduce the need for manual graph management.

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.