Keras
Sequential Models
Model Merging
Deep Learning
Neural Networks

Merge 2 sequential models in Keras

Master System Design with Codemia

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

Introduction

Keras Sequential models are designed for simple layer-by-layer stacks. As soon as you want to combine two branches, concatenate outputs, or feed one model into another in a more flexible way, you should switch to the Functional API. The good news is that you do not have to throw away the sequential submodels. You can build them separately and then wire them together as callable building blocks.

Why Sequential Alone Is Not Enough

A Sequential model assumes one linear path from input to output. That works for stacks such as dense layers or convolution blocks, but not for architectures like:

  • two input branches merged together
  • feature extractors whose outputs are concatenated
  • one submodel reused in multiple places

For those shapes, the Functional API is the right tool.

Merge Two Sequential Models with Concatenation

Suppose you have two feature-extraction branches, each defined as a Sequential model.

python
1from keras import Input, Model, Sequential
2from keras.layers import Dense, Concatenate
3
4left_branch = Sequential([
5    Dense(16, activation="relu"),
6    Dense(8, activation="relu")
7], name="left_branch")
8
9right_branch = Sequential([
10    Dense(16, activation="relu"),
11    Dense(8, activation="relu")
12], name="right_branch")
13
14left_input = Input(shape=(10,), name="left_input")
15right_input = Input(shape=(6,), name="right_input")
16
17left_output = left_branch(left_input)
18right_output = right_branch(right_input)
19
20merged = Concatenate()([left_output, right_output])
21final_output = Dense(1, activation="sigmoid")(merged)
22
23model = Model(inputs=[left_input, right_input], outputs=final_output)
24model.summary()

This is the standard pattern:

  1. define each sequential submodel
  2. create explicit Input tensors
  3. call each submodel on its input
  4. merge the outputs with Concatenate, Add, or another merge layer

Chain One Sequential Model After Another

If the goal is not branching but simply placing one submodel after another, you can still use the same idea.

python
1from keras import Input, Model, Sequential
2from keras.layers import Dense
3
4encoder = Sequential([
5    Dense(32, activation="relu"),
6    Dense(16, activation="relu")
7], name="encoder")
8
9classifier = Sequential([
10    Dense(8, activation="relu"),
11    Dense(3, activation="softmax")
12], name="classifier")
13
14inputs = Input(shape=(20,))
15x = encoder(inputs)
16outputs = classifier(x)
17
18model = Model(inputs, outputs)
19model.summary()

This is useful when you want to reuse a pretrained block or keep model parts logically separate.

Choose the Right Merge Operation

Concatenation is only one option. Keras also supports operations such as addition, averaging, and multiplication when tensor shapes are compatible.

python
1from keras import Input, Model, Sequential
2from keras.layers import Dense, Add
3
4branch_a = Sequential([Dense(8, activation="relu")])
5branch_b = Sequential([Dense(8, activation="relu")])
6
7inp = Input(shape=(12,))
8a = branch_a(inp)
9b = branch_b(inp)
10
11merged = Add()([a, b])
12out = Dense(1)(merged)
13
14model = Model(inp, out)

Use Add only when the branch outputs have the same shape. Use Concatenate when you want to preserve features from both branches side by side.

Reuse Trained Sequential Models

One advantage of this approach is that you can merge models that were trained or designed separately.

For example:

python
left_branch.trainable = False
right_branch.trainable = True

That lets you freeze one branch and fine-tune the other. This is common in transfer learning or multimodal models.

Common Pitfalls

The most common mistake is trying to merge Sequential models directly without explicit inputs. The merge must happen on tensors produced by calling the submodels, not by stacking raw model objects together in a list.

Another issue is output-shape mismatch. Add requires equal shapes, while Concatenate requires compatible dimensions along the chosen axis.

Some developers also forget that once the architecture becomes multi-input or multi-branch, the top-level model should usually be a Functional Model, not another Sequential.

Finally, if you reuse pretrained models, check trainable flags carefully before compiling. Frozen and trainable layers behave differently during optimization.

Summary

  • 'Sequential is good for linear stacks, but merging models is a Functional API task.'
  • You can keep each branch as a Sequential submodel and call it on an Input.
  • Use Concatenate for feature merging and Add only when shapes match.
  • Chaining one sequential submodel after another works the same way.
  • For merged architectures, the final top-level model should usually be built with keras.Model.

Course illustration
Course illustration

All Rights Reserved.