TensorFlow
machine learning
graph duplication
neural networks
deep learning

duplicate a tensorflow 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

Introduction

Duplicating a TensorFlow graph usually means recreating the same computation without accidentally sharing variables. That comes up in target networks, model comparison experiments, and old TensorFlow 1.x code that still manages explicit graphs. The correct approach depends on whether you are using modern Keras-style models or legacy graph objects.

In TensorFlow 2, Clone the Model Structure

In modern TensorFlow, most “graph duplication” really means duplicating a Keras model. The standard tool is tf.keras.models.clone_model, which copies the architecture but does not automatically copy trained weights.

python
1import tensorflow as tf
2
3source = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1),
7])
8
9source.build((None, 4))
10source.set_weights([
11    tf.ones((4, 8)),
12    tf.zeros((8,)),
13    tf.ones((8, 1)),
14    tf.zeros((1,)),
15])
16
17clone = tf.keras.models.clone_model(source)
18clone.build((None, 4))
19clone.set_weights(source.get_weights())
20
21sample = tf.constant([[1.0, 2.0, 3.0, 4.0]])
22print(source(sample).numpy())
23print(clone(sample).numpy())

After set_weights, both models start with the same parameters. But they hold separate variables, so training one does not mutate the other.

Cloning Structure Is Not the Same as Sharing Weights

This distinction matters. If you want two networks to evolve independently, clone the model and copy weights once. If you want two branches to use the same parameters on purpose, reuse the same layer or model objects instead of cloning them.

That is the difference between:

  • an independent target network
  • a shared-weight siamese or multi-branch model

Many TensorFlow bugs come from mixing those two ideas.

tf.function Still Uses Graphs Internally

TensorFlow 2 builds graphs internally when you decorate code with tf.function, but you still should not try to copy low-level graph nodes directly. The cleaner approach is to instantiate separate modules that trace the same computation.

python
1import tensorflow as tf
2
3class ScoringModule(tf.Module):
4    def __init__(self):
5        super().__init__()
6        self.w = tf.Variable([[2.0], [3.0]])
7        self.b = tf.Variable([1.0])
8
9    @tf.function
10    def __call__(self, x):
11        return tf.matmul(x, self.w) + self.b
12
13first = ScoringModule()
14second = ScoringModule()
15
16x = tf.constant([[5.0, 6.0]])
17print(first(x).numpy())
18print(second(x).numpy())

Both modules trace the same math, but they own different variables. That is normally what people mean when they ask for a duplicated graph in TensorFlow 2.

TensorFlow 1.x Requires Rebuilding the Graph

If you still maintain TensorFlow 1.x code, graphs are explicit objects. In that world, duplication means rebuilding the same operations inside a new tf.Graph and then copying variable values if needed.

python
1import tensorflow.compat.v1 as tf
2
3tf.disable_eager_execution()
4
5def build_graph():
6    x = tf.placeholder(tf.float32, shape=[None, 2], name="x")
7    w = tf.Variable([[2.0], [3.0]], name="w")
8    b = tf.Variable([1.0], name="b")
9    y = tf.matmul(x, w) + b
10    return x, y
11
12graph_a = tf.Graph()
13with graph_a.as_default():
14    x_a, y_a = build_graph()
15    init_a = tf.global_variables_initializer()
16
17graph_b = tf.Graph()
18with graph_b.as_default():
19    x_b, y_b = build_graph()
20    init_b = tf.global_variables_initializer()

You do not transplant operations from one graph into another. Each Tensor and Operation belongs to the graph that created it.

Optimizer State Is Separate Too

A common surprise is that cloning a model does not clone optimizer state. If you need the duplicate to continue training exactly from the same moment, you must also save and restore the optimizer or checkpoint the full training state.

That is why a model clone is often enough for inference comparison or target-network initialization, but not enough for seamless training continuation.

Common Pitfalls

  • Assuming clone_model copies weights automatically.
  • Confusing independent clones with intentional shared-weight reuse.
  • Trying to move TensorFlow 1.x operations from one graph into another.
  • Forgetting that optimizer state is separate from model weights.
  • Copying low-level graph concepts in TensorFlow 2 when creating separate modules would be simpler.

Summary

  • In TensorFlow 2, duplicate model structure with tf.keras.models.clone_model.
  • Copy weights separately if the clone should start from the same parameters.
  • Use separate module instances for duplicated tf.function logic.
  • In TensorFlow 1.x, rebuild the graph inside a new tf.Graph instead of copying operations directly.
  • Decide early whether you need independent weights or intentionally shared weights, because the implementation differs.

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.