How to Concatenate Layers in Keras

  • Post category:Keras / Python

In Keras, you can concatenate layers using the Concatenate layer from the Keras functional API. This allows you to combine the output of multiple layers into a single layer. Here’s how to concatenate layers in Keras:

# Import necessary libraries
import keras
from keras.layers import Input, Dense, Concatenate
from keras.models import Model

# Create input layers
input1 = Input(shape=(10,))
input2 = Input(shape=(5,))

# Create some layers for demonstration
dense1 = Dense(32, activation='relu')(input1)
dense2 = Dense(16, activation='relu')(input2)

# Concatenate the layers using the Concatenate layer
concatenated = Concatenate()([dense1, dense2])

# Build the model
model = Model(inputs=[input1, input2], outputs=concatenated)
model.summary()

Output:

Model: "model_1"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_3 (InputLayer)           [(None, 10)]         0           []                               
                                                                                                  
 input_4 (InputLayer)           [(None, 5)]          0           []                               
                                                                                                  
 dense_29 (Dense)               (None, 32)           352         ['input_3[0][0]']                
                                                                                                  
 dense_30 (Dense)               (None, 16)           96          ['input_4[0][0]']                
                                                                                                  
 concatenate_1 (Concatenate)    (None, 48)           0           ['dense_29[0][0]',               
                                                                  'dense_30[0][0]']               
                                                                                                  
==================================================================================================
Total params: 448
Trainable params: 448
Non-trainable params: 0
__________________________________________________________________________________________________

In this example:

  1. We create input layers for two sets of data with different shapes.
  2. We apply some layers (e.g., Dense layers) to each input independently.
  3. We use the Concatenate layer to concatenate the outputs of the previous layers.
  4. Finally, we create a Keras model using the functional API with the concatenated layer as the output.

This example illustrates how to concatenate layers in Keras, a useful technique when you want to combine features from different parts of your neural network or when building multi-input models.