tensorflow keras
model concatenation
deep learning
neural networks
machine learning

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.

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5left_input = keras.Input(shape=(8,), name="left_features")
6left_branch = layers.Dense(16, activation="relu")(left_input)
7left_branch = layers.Dense(8, activation="relu")(left_branch)
8
9right_input = keras.Input(shape=(4,), name="right_features")
10right_branch = layers.Dense(8, activation="relu")(right_input)
11
12merged = layers.Concatenate()([left_branch, right_branch])
13output = layers.Dense(1, activation="sigmoid")(merged)
14
15model = keras.Model(inputs=[left_input, right_input], outputs=output)
16model.summary()

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.

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5vision_input = keras.Input(shape=(32,), name="vision_input")
6vision_output = layers.Dense(16, activation="relu")(vision_input)
7vision_model = keras.Model(vision_input, vision_output, name="vision_model")
8
9meta_input = keras.Input(shape=(6,), name="meta_input")
10meta_output = layers.Dense(8, activation="relu")(meta_input)
11meta_model = keras.Model(meta_input, meta_output, name="meta_model")
12
13combined = layers.Concatenate()([vision_model.output, meta_model.output])
14final_output = layers.Dense(3, activation="softmax")(combined)
15combined_model = keras.Model(
16    inputs=[vision_model.input, meta_model.input],
17    outputs=final_output,
18)

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.

python
pooled = layers.GlobalAveragePooling2D()(conv_branch)
merged = layers.Concatenate()([pooled, dense_branch])

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.

python
1import numpy as np
2
3x_left = np.random.rand(20, 8).astype("float32")
4x_right = np.random.rand(20, 4).astype("float32")
5y = np.random.randint(0, 2, size=(20, 1)).astype("float32")
6
7model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
8model.fit([x_left, x_right], y, epochs=3, batch_size=4)

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 Sequential container and expecting feature concatenation.
  • Forgetting that Concatenate merges 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.

Course illustration
Course illustration

All Rights Reserved.