TensorFlow
model integration
sequential models
machine learning
deep learning

How to sequentially combine 2 tensorflow models?

Master System Design with Codemia

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

Introduction

Combining two TensorFlow models sequentially means feeding the output of the first model directly into the second. This is common when one model acts as a feature extractor and the next model performs classification, regression, or some other downstream task.

The Key Requirement: Shape Compatibility

Before connecting models, verify that the output shape of model A matches the input shape expected by model B. If the shapes do not line up, you need an adapter layer such as Dense, Flatten, Reshape, or a normalization step in between.

Here is a small example with a feature extractor and a classifier:

python
1import tensorflow as tf
2
3feature_extractor = tf.keras.Sequential(
4    [
5        tf.keras.layers.Input(shape=(20,)),
6        tf.keras.layers.Dense(32, activation="relu"),
7        tf.keras.layers.Dense(16, activation="relu"),
8    ],
9    name="feature_extractor",
10)
11
12classifier = tf.keras.Sequential(
13    [
14        tf.keras.layers.Input(shape=(16,)),
15        tf.keras.layers.Dense(8, activation="relu"),
16        tf.keras.layers.Dense(3, activation="softmax"),
17    ],
18    name="classifier",
19)

The output of the first model has shape 16, which matches the input of the second model.

Connecting the Models With the Functional API

The simplest way to combine them is to call one model on an input tensor and feed the result into the other model:

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(20,))
4x = feature_extractor(inputs)
5outputs = classifier(x)
6
7combined_model = tf.keras.Model(inputs=inputs, outputs=outputs, name="combined")
8combined_model.compile(
9    optimizer="adam",
10    loss="sparse_categorical_crossentropy",
11    metrics=["accuracy"],
12)
13
14combined_model.summary()

Even if the original models were created with Sequential, the combined version is usually easiest to express with the Functional API because it makes the data flow explicit.

Freezing or Fine-Tuning the First Model

Often the first model is pretrained. In that case, you may want to freeze it initially:

python
1feature_extractor.trainable = False
2
3combined_model.compile(
4    optimizer="adam",
5    loss="sparse_categorical_crossentropy",
6    metrics=["accuracy"],
7)

Freezing keeps the pretrained weights fixed while the second model learns. Later, you can unfreeze some or all of the first model and fine-tune the whole stack with a smaller learning rate.

Adding an Adapter Layer Between Models

If the shapes do not match, add a small adapter:

python
1inputs = tf.keras.Input(shape=(20,))
2x = feature_extractor(inputs)
3x = tf.keras.layers.Dense(64, activation="relu")(x)
4outputs = classifier(x)
5
6adjusted_model = tf.keras.Model(inputs=inputs, outputs=outputs)

This is also useful when the first model emits embeddings and the second model expects a richer feature representation.

Training and Saving the Combined Model

Once the combined model is built, it behaves like any other Keras model:

python
1import numpy as np
2
3x_train = np.random.rand(100, 20).astype("float32")
4y_train = np.random.randint(0, 3, size=(100,))
5
6combined_model.fit(x_train, y_train, epochs=3, batch_size=16)
7combined_model.save("combined_model.keras")

Saving the combined model is often easier than trying to coordinate two separate artifacts at inference time.

If you still need to inspect intermediate features later, keep a separate model that ends at the feature extractor output. That gives you both a production-ready combined graph and a debugging tool for embeddings or activations.

Common Pitfalls

  • Forgetting to check tensor shapes is the most common reason model composition fails.
  • Combining two compiled models does not preserve the training setup you want, so recompile the final combined model.
  • Freezing a model after compiling can be confusing because you generally need to compile again for the change to take effect cleanly.
  • Mixing preprocessing assumptions between the two models can produce poor predictions even if the shapes line up.
  • Saving only one component model can make deployment harder than saving the combined graph.

Summary

  • Sequential model composition means feeding model A output into model B input.
  • Verify shapes first and add adapter layers when needed.
  • Use the Functional API to connect existing models clearly.
  • Freeze pretrained layers when appropriate, then recompile the combined model.
  • Train, evaluate, and save the combined result as one model for simpler inference.

Course illustration
Course illustration

All Rights Reserved.