Tensorflow
Keras
Model Weights
Deep Learning
Machine Learning

Tensorflow Keras Copy Weights From One Model to Another

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

Copying weights between Keras models is common in transfer learning, architecture refactoring, and controlled experiments. The key rule is that weight copying transfers parameter values, not model topology, so the source and target layers must exist and have compatible shapes before the assignment can succeed.

Copy All Weights When Architectures Match

If two models have the same layer structure and weight shapes, the simplest approach is get_weights() plus set_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
9target = tf.keras.Sequential([
10    tf.keras.layers.Input(shape=(4,)),
11    tf.keras.layers.Dense(8, activation="relu"),
12    tf.keras.layers.Dense(1)
13])
14
15source(tf.ones((1, 4)))
16target(tf.ones((1, 4)))
17
18target.set_weights(source.get_weights())

The forward call before set_weights is important because subclassed or lazily built models need their variables created before weights can be assigned.

Verify That the Copy Worked

A quick numerical check is better than trusting the absence of an exception.

python
1import tensorflow as tf
2
3x = tf.constant([[1.0, 2.0, 3.0, 4.0]])
4
5source_output = source(x)
6target_output = target(x)
7
8print(tf.reduce_all(tf.equal(source_output, target_output)).numpy())

If both models have the same structure and weights, they should produce the same output for the same input.

Copy Layer by Layer When the Models Differ

Sometimes you only want to transfer part of a model, such as a shared feature extractor. In that case, copy layer weights explicitly.

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", name="encoder"),
6    tf.keras.layers.Dense(1, name="head")
7])
8
9target = tf.keras.Sequential([
10    tf.keras.layers.Input(shape=(4,)),
11    tf.keras.layers.Dense(8, activation="relu", name="encoder"),
12    tf.keras.layers.Dense(3, activation="softmax", name="classifier")
13])
14
15source(tf.ones((1, 4)))
16target(tf.ones((1, 4)))
17
18target.get_layer("encoder").set_weights(
19    source.get_layer("encoder").get_weights()
20)

This pattern is common when you reuse an encoder or backbone but replace the output head for a new task.

Use Cloning for Architecture Reuse

If you want the same architecture and then copy the weights, cloning the model can reduce manual duplication.

python
1import tensorflow as tf
2
3original = 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
9original(tf.ones((1, 4)))
10
11cloned = tf.keras.models.clone_model(original)
12cloned(tf.ones((1, 4)))
13cloned.set_weights(original.get_weights())

Cloning reproduces the structure, but it does not copy learned weights automatically. You still need the explicit set_weights step.

Shape Compatibility Is Non-Negotiable

Keras can only assign weights when the target layer expects arrays with the same shapes as the source. If one dense layer has 8 units and the other has 16, direct copying fails.

That means you can usually copy between:

  • identical architectures
  • matching layers inside slightly different models
  • renamed models with equivalent weight shapes

But you cannot directly copy between incompatible layers without writing custom conversion logic.

Common Pitfalls

One common mistake is calling set_weights before the target model is built. If the target variables do not exist yet, Keras cannot assign anything.

Another is assuming architecture copying and weight copying are the same operation. They are separate. clone_model copies structure, while set_weights copies parameter values.

Developers also sometimes try to copy weights by layer order when the real match is by semantic role. If the models diverged, matching by layer name is often safer than matching by position.

Finally, even when weight assignment succeeds, confirm the behavior with a forward pass. A silent mismatch in preprocessing or input shape can make the copied weights less useful than expected.

Summary

  • Use get_weights() and set_weights() when the source and target architectures match.
  • Build both models before assigning weights.
  • Copy layer by layer when only part of the model should be transferred.
  • 'clone_model copies topology, not learned parameters.'
  • Weight shapes must be compatible for copying to work.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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.