0

I am trying to concatenate two layers by using Keras and TensorFlow.I keep getting the error at the concatenate line. I have tried other suggestions from similar questions but it keeps giving the same error. I feel that I'm doing some mistakes but I can't figure it out. can anyone explain this?

model1=Sequential()
model1.add(LSTM(150,return_sequences=True,input_shape=(win_length,num_features), activation='relu'))
model1.add(LSTM(100, return_sequences=False))
model1.add(BatchNormalization())

model2=Sequential()
model2.add(GRU(150, return_sequences=True, input_shape=(win_length,num_features), activation='relu' ))
model2.add(GRU(100, return_sequences=False))
model2.add(BatchNormalization())

merged_model = Sequential()
merged_model.add(Concatenate([model1, model2]))
merged_model.add(Dense(300))
merged_model.add(PReLU())
merged_model.add(Dropout(0.2))

#merged_model.summary()

merged_model.compile(loss='mse', optimizer='adam')
merged_model.fit(train_generator, epochs=50, batch_size=32)

this is the error:

ValueError: Exception encountered when calling layer "sequential_114" (type Sequential).

A `Concatenate` layer should be called on a list of at least 1 input. Received: input_shape=(32, 10, 4)

Call arguments received:
  • inputs=tf.Tensor(shape=(32, 10, 4), dtype=float32)
  • training=False
  • mask=None

I feel there are some updates in Keras, that are not letting two different models concatenate properly.

Julia
  • 3
  • 4
  • From my understanding, models cannot be concatenated, but you can concatenate the layers within the models. For example, you can have a model that has multiple inputs and multiple outputs, which can evaluate loss functions for each output. So you can do what you're trying to do, just not by concatenating the models themselves. Here are some helpful links: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Concatenate https://stackoverflow.com/questions/57202016/concatenate-two-models-with-tensorflow-keras – Pritam Dodeja Jun 03 '22 at 19:08
  • Yes this is not how you use Concatenate, it is a Functional API and you cannot use in a Sequential API. – Dr. Snoopy Jun 03 '22 at 19:14
  • @PritamDodeja extremely sorry for the miscommunication, it was layers, not models. Thanks for sharing the link. Do u have any idea how to do this for layers? it is giving error on input_shape. – Julia Jun 03 '22 at 19:14
  • Can you edit the question with some input data? I will try and solve this tonight. – Pritam Dodeja Jun 03 '22 at 19:17
  • What do you mean exactly by merge two models? – Dr. Snoopy Jun 03 '22 at 19:20
  • @PritamDodeja i have added a picture. is it enough? – Julia Jun 03 '22 at 19:22
  • @Dr.Snoopy sorry I meant to concatenate two layers, not models. I am new to ML that's why I am not that familiar. – Julia Jun 03 '22 at 19:24
  • You cannot really merge layers, what you can do is merge the outputs of those layers, and for that you need to use the Functional API, see: https://keras.io/guides/functional_api/ – Dr. Snoopy Jun 03 '22 at 19:50
  • @Julia definitely go through what Dr.Snoopy has posted. If you are still struggling with it after having gone through that, I'll work with you to solve it. You'll gain a lot more by going through the path Dr.Snoopy is suggesting. – Pritam Dodeja Jun 04 '22 at 00:41

0 Answers0