Keras
concatenate layers
neural networks
deep learning
machine learning

How to concatenate two layers in keras?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In Keras, concatenating two layers means taking the outputs of two branches and joining them along a chosen axis. This is common in multi-input models, skip connections, and architectures that combine learned features from different paths.

Use the Functional API

Layer concatenation is a graph-building operation, so the Functional API is the natural fit. The key requirement is that the tensors must match in every dimension except the one you are concatenating on.

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

In this example, both branches end with shape (None, 64), so concatenating on the last axis produces shape (None, 128).

What Has to Match

Concatenation does not magically resize tensors. If one branch has shape (None, 64) and the other has shape (None, 32), that is fine when concatenating on the last axis because all other dimensions match.

But if the shapes are (None, 10, 64) and (None, 8, 64), concatenating on the last axis will fail because the middle dimension does not match.

So the rule is simple:

  • all dimensions except the concat axis must be equal

That is the first thing to check when Keras raises a shape mismatch error.

Concatenating Convolutional Branches

The same pattern works with convolutional layers as long as the spatial dimensions line up.

python
1from tensorflow.keras.layers import Concatenate, Conv2D, Input
2from tensorflow.keras.models import Model
3
4inputs = Input(shape=(64, 64, 3))
5
6branch_1 = Conv2D(16, kernel_size=3, padding="same", activation="relu")(inputs)
7branch_2 = Conv2D(16, kernel_size=5, padding="same", activation="relu")(inputs)
8
9merged = Concatenate(axis=-1)([branch_1, branch_2])
10model = Model(inputs=inputs, outputs=merged)
11model.summary()

Here the height and width match because both convolutions use padding="same". The channel dimension is what grows after concatenation.

You Can Also Use the Functional Shortcut

Keras also offers a functional helper:

python
from tensorflow.keras.layers import concatenate

merged = concatenate([left_branch, right_branch], axis=-1)

This is mostly a style choice. Concatenate(...) as a layer is often clearer in larger models because it looks like the rest of the graph-building code.

Concatenation Versus Addition

Do not confuse concatenation with merging by addition. Concatenation keeps both feature sets side by side and increases dimensionality. Addition requires matching shapes and combines values element-wise.

That difference matters architecturally:

  • concatenation preserves separate feature channels
  • addition fuses channels into the same shape

Pick the merge operation that matches the model design you actually want.

Concatenation is also common when mixing different feature types, such as dense numeric features plus an embedding branch. In those designs, each branch can learn separately first, then the model merges them into one shared representation for later layers and a final prediction head.

Common Pitfalls

  • Trying to concatenate tensors whose non-concatenation dimensions do not match.
  • Using Sequential for a model that really needs branching and merging.
  • Confusing concatenation with element-wise addition.
  • Forgetting that concatenation increases dimensionality along the selected axis.
  • Mismatching convolution output sizes by changing stride or padding in one branch only.

Summary

  • Use the Keras Functional API to concatenate layer outputs.
  • 'Concatenate joins tensors along a chosen axis.'
  • All dimensions except the concat axis must match.
  • Concatenation is common in multi-input and multi-branch models.
  • If the shapes do not line up, fix the branch outputs before merging them.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.