0

I would like to concatenate three layers in my Keras sequential model. Is there a way to do so? I would like to concatenate them along axis=2.

Here is how the model summary looks like for now.

Here is the code.

model = Sequential()
model.add(keras.layers.InputLayer(input_shape=(seq_len, n_inputs)))
model.add(keras.layers.Conv1D(input_shape=(None, n_inputs,seq_len), filters=N_CONV_A, padding='same',kernel_size=F_CONV_A, strides=1, activation='relu'))
model.add(keras.layers.Conv1D(input_shape=(None, n_inputs,seq_len), filters=N_CONV_B, padding='same',kernel_size=F_CONV_B, strides=1, activation='relu'))
model.add(keras.layers.Conv1D(input_shape=(None, n_inputs,seq_len), filters=N_CONV_C, padding='same',kernel_size=F_CONV_C, strides=1, activation='relu'))
Shaido
  • 27,497
  • 23
  • 70
  • 73
palutuna
  • 9
  • 1
  • 5
  • What do you mean by "concatenate the layers". Do you mean you want to concatenate the output of the three layers? – Aditya Mehrotra Jul 08 '21 at 04:25
  • Hi Aditya, yes I do. Sorry for not clarifying it earlier. – palutuna Jul 08 '21 at 04:26
  • Simply sequential models, are limited to one input and one output, and as it's name suggests, you can arrange layers in sequential. If you have 3 layers in parallel and the same input, you should use Functional API or model subclassing. – Kaveh Jul 08 '21 at 07:11

1 Answers1

3

A sequential model cannot manage concatenation because the layers that are concatenated are computed in parallel with respect to its input.

In a sequential model the output of a layer becomes the input of the next layer, while the concatenation requires to use the same input for many layers.

In this situation you should use the Functional API or Model subclassing.

For example using the functional API the code becomes

inputs = keras.layers.Input(shape=(n_inputs, seq_len))
convA = keras.layers.Conv1D(input_shape=(None, n_inputs, seq_len),filters=N_CONV_A, padding='same',kernel_size=F_CONV_A, strides=1, activation='relu')
convB = keras.layers.Conv1D(input_shape=(None, n_inputs, seq_len),filters=N_CONV_B, padding='same',kernel_size=F_CONV_B, strides=1, activation='relu')
convC = keras.layers.Conv1D(input_shape=(None, n_inputs, seq_len),filters=N_CONV_C, padding='same',kernel_size=F_CONV_C, strides=1, activation='relu')
concat = keras.layers.Concatenate(axis=-1)
out = concat([convA(inputs), convB(inputs), convC(inputs)])

Attention: input_shape in the your code is not consistent with the expected ones.

LGrementieri
  • 760
  • 4
  • 12