title: Keras: How to concatenate two CNN?
body: <p>I'm trying to implement the CNN model in this article (<a href="https://arxiv.org/abs/1605.07333" rel="nofollow noreferrer">https://arxiv.org/abs/1605.07333</a>) </p>
  
  <p>Here, they have two different contexts as inputs which are processed by two independent conv and max-pooling layers. After pooling they concat the results. </p>
  
  <p><a href="https://i.stack.imgur.com/VIIy6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VIIy6.png" alt="CNNs"></a></p>
  
  <p>Assuming each CNN is modelled as such, how do I achieve the model above?</p>
  
  <pre><code>def baseline_cnn(activation='relu'):
  
  model = Sequential()
  model.add(Embedding(SAMPLE_SIZE, EMBEDDING_DIMS, input_length=MAX_SMI_LEN))
  model.add(Dropout(0.2))
  model.add(Conv1D(NUM_FILTERS, FILTER_LENGTH, padding='valid', activation=activation, strides=1))
  model.add(GlobalMaxPooling1D())
  model.add(Dense(1))
  model.add(Activation('sigmoid'))
  model.compile(loss='binary_crossentropy', optimizer='adam',  metrics=['accuracy'])
  
  return model
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Final Code:</strong> I simply used @FernandoOrtega's solution:</p>
  
  <pre class="lang-py prettyprint-override"><code>def build_combined(FLAGS, NUM_FILTERS, FILTER_LENGTH1, FILTER_LENGTH2):
      Dinput = Input(shape=(FLAGS.max_dlen, FLAGS.dset_size))
      Tinput = Input(shape=(FLAGS.max_tlen, FLAGS.tset_size))
  
  
      encode_d= Conv1D(filters=NUM_FILTERS, kernel_size=FILTER_LENGTH1,  activation='relu', padding='valid',  strides=1)(Dinput)
      encode_d = Conv1D(filters=NUM_FILTERS*2, kernel_size=FILTER_LENGTH1,  activation='relu', padding='valid',  strides=1)(encode_d)
      encode_d = GlobalMaxPooling1D()(encode_d)
  
      encode_tt = Conv1D(filters=NUM_FILTERS, kernel_size=FILTER_LENGTH2,  activation='relu', padding='valid',  strides=1)(Tinput)
      encode_tt = Conv1D(filters=NUM_FILTERS*2, kernel_size=FILTER_LENGTH1,  activation='relu', padding='valid',  strides=1)(encode_tt)
      encode_tt = GlobalMaxPooling1D()(encode_tt)
  
      encode_combined = keras.layers.concatenate([encode_d, encode_tt])
  
  
      # Fully connected 
      FC1 = Dense(1024, activation='relu')(encode_combined)
      FC2 = Dropout(0.1)(FC1)
      FC2 = Dense(512, activation='relu')(FC2)
  
      predictions = Dense(1, kernel_initializer='normal')(FC2) 
  
      combinedModel = Model(inputs=[Dinput, Tinput], outputs=[predictions])
      combinedModel.compile(optimizer='adam', loss='mean_squared_error', metrics=[accuracy])
  
      print(combinedModel.summary())
  
      return combinedModel
  </code></pre>