TensorFlow
deep learning
machine learning
variable transfer
graph manipulation

Copy variables from one TensorFlow graph to another

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Copying variables from one TensorFlow graph to another usually means transferring learned weights, not cloning an entire runtime state blindly. In TensorFlow 1 style graph mode, the two normal approaches are checkpoint-based restore and explicit assignment between matching variables; in modern TensorFlow 2 code, object-level weight transfer is usually simpler.

First Check What "Matches"

Before copying anything, verify three things about the source and target variables:

  • the variables represent the same conceptual weights,
  • the shapes match,
  • the dtypes match.

If those conditions are not true, copying will either fail or produce a misconfigured model.

This topic mostly belongs to TensorFlow 1 graph mode, where separate tf.Graph objects and sessions are explicit. In TensorFlow 2, you often should not be working at this level unless you are maintaining legacy code.

Easiest Path: Save and Restore

If the target graph defines variables with the same names and shapes, checkpoint restore is usually the cleanest method.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5source_graph = tf.Graph()
6with source_graph.as_default():
7    w = tf.compat.v1.get_variable("dense/kernel", shape=[2, 2], initializer=tf.ones_initializer())
8    b = tf.compat.v1.get_variable("dense/bias", shape=[2], initializer=tf.zeros_initializer())
9    saver = tf.compat.v1.train.Saver()
10
11with tf.compat.v1.Session(graph=source_graph) as sess:
12    sess.run(tf.compat.v1.global_variables_initializer())
13    saver.save(sess, "/tmp/source.ckpt")

Then restore into the target graph:

python
1target_graph = tf.Graph()
2with target_graph.as_default():
3    w2 = tf.compat.v1.get_variable("dense/kernel", shape=[2, 2])
4    b2 = tf.compat.v1.get_variable("dense/bias", shape=[2])
5    saver2 = tf.compat.v1.train.Saver()
6
7with tf.compat.v1.Session(graph=target_graph) as sess:
8    sess.run(tf.compat.v1.global_variables_initializer())
9    saver2.restore(sess, "/tmp/source.ckpt")
10    print(sess.run(w2))

This works well because the checkpoint provides a stable transfer mechanism and TensorFlow already knows how to match variables by name.

Explicit Assignment Between Live Graphs

If you already have two sessions open and want to copy values directly, read from the source session and assign into the target session.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5g1 = tf.Graph()
6with g1.as_default():
7    src = tf.compat.v1.get_variable("weight", initializer=[[1.0, 2.0], [3.0, 4.0]])
8
9g2 = tf.Graph()
10with g2.as_default():
11    dst = tf.compat.v1.get_variable("weight", shape=[2, 2])
12    placeholder = tf.compat.v1.placeholder(tf.float32, shape=[2, 2])
13    assign_dst = dst.assign(placeholder)
14
15with tf.compat.v1.Session(graph=g1) as s1, tf.compat.v1.Session(graph=g2) as s2:
16    s1.run(tf.compat.v1.global_variables_initializer())
17    s2.run(tf.compat.v1.global_variables_initializer())
18
19    src_value = s1.run(src)
20    s2.run(assign_dst, feed_dict={placeholder: src_value})
21
22    print(s2.run(dst))

This is useful when names differ or when only a subset of variables should be copied. It also makes the transfer contract very explicit, which is helpful during graph refactors.

TensorFlow 2 Equivalent

In TensorFlow 2, weight transfer is usually much simpler because models expose get_weights() and set_weights().

python
1import tensorflow as tf
2
3model_a = tf.keras.Sequential([
4    tf.keras.layers.Dense(4, input_shape=(3,))
5])
6
7model_b = tf.keras.Sequential([
8    tf.keras.layers.Dense(4, input_shape=(3,))
9])
10
11_ = model_a(tf.zeros((1, 3)))
12_ = model_b(tf.zeros((1, 3)))
13
14model_b.set_weights(model_a.get_weights())

If you are writing new code, this is usually the better design. Manual graph-to-graph copying is mainly a legacy TensorFlow 1 concern.

Common Pitfalls

  • Trying to copy variables without confirming matching shapes and dtypes first.
  • Assuming two graphs can share weights automatically just because their layer intent is similar.
  • Mixing TensorFlow 1 graph-mode techniques into TensorFlow 2 code that should use object-level weight transfer instead.
  • Restoring by name after refactoring variable scopes and then wondering why variables are missing.
  • Forgetting that each session belongs to a specific graph and accidentally reading or assigning in the wrong context.

Summary

  • In TensorFlow 1 graph mode, checkpoint restore is usually the cleanest way to copy matching variables between graphs.
  • Direct assignment is useful when you need custom mapping or already have both sessions alive.
  • Variable names, shapes, and dtypes must match the intended transfer contract.
  • In TensorFlow 2, prefer get_weights() and set_weights() over manual graph manipulation.
  • Always verify what "matching variables" means before copying learned weights between models.

Course illustration
Course illustration

All Rights Reserved.