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:
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:
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:
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:
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:
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.

