0

I have 2 different set of features, for which I have created 2 separate Sequential models, something like this:

model1 = Sequential()
model1.add(Embedding(self.max_words, self.embedding_dim))
model1.add(LSTM(32))

and

model2 = Sequential()
model2.add(Dense(64, activation='relu', input_shape=(num_features,)))
model2.add(Dense(32, activation='relu'))

Finally, I'm concatenating these 2 models like this:

merged_model = Sequential()
merged_model.add(Concatenate([model1, model2]))
merged_model.add(Dense(1, activation='sigmoid'))
merged_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

However, I'm confused about what the 'fit' code would look like, as we need to have 2 separate x_train datasets in merged_model. Is this the correct way to do this:

history = merged_model.fit([x_train1, x_train2], y_train, epochs=10, batch_size=128, validation_split=0.2)

Also, is this the correct way to concatenate 2 layers if we want to merge 2 parallel models ?

Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84
  • The sequential API allows you to create models layer-by-layer but is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs. Use Model API for your purpose. https://stackoverflow.com/questions/51200361/constructing-a-keras-model – Deven Aug 21 '19 at 07:09
  • 1
    So basically you're looking for multiple-inputs but single-output, right? – Arsal Aug 21 '19 at 07:28
  • 1
    You can't use Sequential API here for merging two models. See [this answer](https://stackoverflow.com/a/53017742/2099607) for the reason, and [this answer](https://stackoverflow.com/a/52234056/2099607) for a sample of using Functional API for merging models. – today Aug 21 '19 at 07:45
  • As for the inputs of `fit` method, you are correct: since the final model has two input layers, therefore you need to pass a **list** of two separate input arrays corresponding to each input layer to `fit` method. – today Aug 21 '19 at 07:47

0 Answers0