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.
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.
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.
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:
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
Sequentialfor 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.
- '
Concatenatejoins 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
- How to concatenate two tensors horizontally in TensorFlow?
- How to connect LSTM layers in Keras, RepeatVector or return_sequenceTrue?
- How to construct a network with two inputs in PyTorch
- How to continue training model using ModelCheckpoint of Keras
- how to control frequency of loss logging messages when using tf.Estimator?
- How to control GPU memory size with tf.estimator
- How to continue to train SVM based on the previous model
- How to control tensorflow's VLOG?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.