Concatenate two models with tensorflow.keras
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In tensorflow.keras, you do not usually concatenate whole models as opaque objects. You build submodels or branches, take their output tensors, combine those tensors with keras.layers.Concatenate, and then continue building the final network. This is the standard pattern for feature fusion, multi-input models, and late-stage model merging.
Concatenate branch outputs with the Functional API
The Functional API is the right tool because concatenation joins tensors, not sequential stacks of layers.
This creates two branches with separate inputs and merges their learned features before the final prediction layer.
Use concatenation when the branches learn different signals
Concatenation is useful when each branch captures complementary information. Typical examples include:
- image features plus tabular metadata
- text embeddings plus handcrafted features
- two subnetworks processing different views of the same entity
The point is not just to make the model larger. The point is to fuse representations that would be weaker alone.
You can also merge existing submodels
If you already built reusable submodels, call them on new inputs and concatenate their outputs.
This is the practical meaning of "concatenating two models" in Keras.
Match tensor ranks and compatible shapes
Concatenate joins tensors along one axis, so the other dimensions must match. If one branch produces shape (batch, 16) and another produces (batch, 8), concatenating along the feature axis is fine. If one branch is still a 2D feature vector and the other is a 4D image map, you must reshape or pool first.
Most concatenation errors come from mismatched tensor shapes, not from the idea of multiple models itself.
Compile and train with aligned inputs
When the model has multiple inputs, training data must be passed in the same structure.
If you swap the inputs or pass shapes that do not match the declared inputs, training will fail immediately.
Common Pitfalls
- Trying to put two complete models into a
Sequentialcontainer and expecting feature concatenation. - Forgetting that
Concatenatemerges tensors with matching ranks and compatible non-concatenated dimensions. - Combining branches with different spatial shapes without pooling or reshaping first.
- Passing training data in the wrong input order for a multi-input model.
- Concatenating branches that do not contribute distinct information and only add unnecessary complexity.
Summary
- In Keras, concatenate output tensors, not model objects as black boxes.
- Use the Functional API for branch fusion and multi-input architectures.
- Make sure tensor shapes are compatible before concatenation.
- Train the merged model with inputs passed in the same structure as the model definition.
- Use concatenation when the branches provide complementary information, not just to increase model size.

