What is the difference between concatenate and add 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, Concatenate and Add both combine tensors, but they do very different jobs. Concatenate joins feature maps side by side and changes the tensor shape, while Add performs element-wise addition and keeps the shape the same.
What Concatenate Does
Use Concatenate when you want to keep information from multiple branches by stacking their features along a chosen axis. This is common in multi-input models, encoder-decoder connections, and architectures that merge learned representations from different sources.
The important rule is that all dimensions must match except the concatenation axis.
In that example, the two branches become one larger feature vector. The model can learn how to use information from both branches because none of the features are discarded.
What Add Does
Add works element by element. It sums tensors of the same shape and returns another tensor with that same shape. This is the operation used in residual connections because it combines branches without increasing the number of features.
Because the shapes match, Keras can add corresponding values directly. If one tensor has shape (None, 32) and another has shape (None, 64), Add will fail because there is no one-to-one correspondence between elements.
Shape Difference Is the Real Distinction
The easiest way to remember the difference is this:
- '
Concatenateincreases a dimension' - '
Addpreserves the dimension'
That difference affects the model design. Concatenation increases the representation size, which can make the next layer more expressive but also more expensive. Addition blends two representations into one tensor of the same size, which is cheaper and often better for skip connections.
A Side-by-Side Example
The following example shows both operations with compatible shapes:
Here, added has shape (None, 16) and concatenated has shape (None, 32). That single difference often determines which layer is appropriate.
When to Choose Each One
Choose Concatenate when each branch contains different information and you want the next layer to inspect all of it. This is common when combining text features with numeric features, or low-level image features with high-level image features.
Choose Add when the branches represent compatible feature spaces and you want a residual-style merge. That pattern helps gradients flow through deeper networks and avoids growing the feature dimension at every merge point.
Common Pitfalls
The most common mistake is using Add on tensors with different shapes. Keras will raise a shape error because element-wise addition needs matching dimensions.
Another mistake is using Concatenate where residual behavior was intended. Concatenation does not blend two representations; it preserves both and makes the tensor wider. That changes the parameter count of later layers and can alter the architecture more than expected.
Axis choice is another source of bugs. For dense layers, concatenation usually happens on the last axis. For convolutional models, the correct axis depends on whether the data format is channels-last or channels-first.
Finally, be mindful of memory cost. Repeated concatenation can make tensors much larger, especially in image models. If the design only needs a skip connection, Add is often the more efficient operation.
Summary
- '
Concatenatejoins tensors and increases the size of one axis.' - '
Addperforms element-wise summation and keeps the tensor shape unchanged.' - Use
Concatenateto preserve separate feature sets from different branches. - Use
Addfor residual connections and same-shape feature merging. - Shape compatibility is the first thing to check when either layer fails.
Related reading
- What is the difference between conv1d with kernel_size1 and dense layer?
- What is the difference between CuDNNLSTM and LSTM in Keras?
- What is the difference between different kernel sizes1x1, 3x3, 5x5 in a convolution neural network?
- what is the difference between Flatten and GlobalAveragePooling2D in keras
- What is the difference between Dataset.from_tensors and Dataset.from_tensor_slices?
- What is the difference between keras and tf.keras?
- What is the difference between .flatten and .view-1 in PyTorch?
- What is the difference between Keras and tf.keras in TensorFlow 1.1?
.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.