Keras
deep learning
neural networks
concatenate
add

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.

Practice ML system design

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.

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

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.

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5inputs = keras.Input(shape=(32,))
6
7x = layers.Dense(32, activation="relu")(inputs)
8residual = layers.Dense(32)(x)
9
10combined = layers.Add()([x, residual])
11outputs = layers.Activation("relu")(combined)
12
13model = keras.Model(inputs=inputs, outputs=outputs)
14model.summary()

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:

  • 'Concatenate increases a dimension'
  • 'Add preserves 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:

python
1from tensorflow import keras
2from tensorflow.keras import layers
3
4inputs = keras.Input(shape=(16,))
5
6a = layers.Dense(16, activation="relu")(inputs)
7b = layers.Dense(16, activation="relu")(inputs)
8
9added = layers.Add(name="added_features")([a, b])
10concatenated = layers.Concatenate(name="stacked_features")([a, b])
11
12model = keras.Model(inputs=inputs, outputs=[added, concatenated])
13model.summary()

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

  • 'Concatenate joins tensors and increases the size of one axis.'
  • 'Add performs element-wise summation and keeps the tensor shape unchanged.'
  • Use Concatenate to preserve separate feature sets from different branches.
  • Use Add for residual connections and same-shape feature merging.
  • Shape compatibility is the first thing to check when either layer fails.

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.