How to concatenate two layers in keras? How to concatenate two layers in keras? python python

How to concatenate two layers in keras?


You're getting the error because result defined as Sequential() is just a container for the model and you have not defined an input for it.

Given what you're trying to build set result to take the third input x3.

first = Sequential()first.add(Dense(1, input_shape=(2,), activation='sigmoid'))second = Sequential()second.add(Dense(1, input_shape=(1,), activation='sigmoid'))third = Sequential()# of course you must provide the input to result which will be your x3third.add(Dense(1, input_shape=(1,), activation='sigmoid'))# lets say you add a few more layers to first and second.# concatenate themmerged = Concatenate([first, second])# then concatenate the two outputsresult = Concatenate([merged,  third])ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)result.compile(optimizer=ada_grad, loss='binary_crossentropy',               metrics=['accuracy'])

However, my preferred way of building a model that has this type of input structure would be to use the functional api.

Here is an implementation of your requirements to get you started:

from keras.models import Modelfrom keras.layers import Concatenate, Dense, LSTM, Input, concatenatefrom keras.optimizers import Adagradfirst_input = Input(shape=(2, ))first_dense = Dense(1, )(first_input)second_input = Input(shape=(2, ))second_dense = Dense(1, )(second_input)merge_one = concatenate([first_dense, second_dense])third_input = Input(shape=(1, ))merge_two = concatenate([merge_one, third_input])model = Model(inputs=[first_input, second_input, third_input], outputs=merge_two)ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)model.compile(optimizer=ada_grad, loss='binary_crossentropy',               metrics=['accuracy'])

To answer the question in the comments:

  1. How are result and merged connected? Assuming you mean how are they concatenated.

Concatenation works like this:

  a        b         ca b c   g h i    a b c g h id e f   j k l    d e f j k l

i.e rows are just joined.

  1. Now, x1 is input to first, x2 is input into second and x3 input into third.


Adding to the above-accepted answer so that it helps those who are using tensorflow 2.0

import tensorflow as tf# some datac1 = tf.constant([[1, 1, 1], [2, 2, 2]], dtype=tf.float32)c2 = tf.constant([[2, 2, 2], [3, 3, 3]], dtype=tf.float32)c3 = tf.constant([[3, 3, 3], [4, 4, 4]], dtype=tf.float32)# bake layers x1, x2, x3x1 = tf.keras.layers.Dense(10)(c1)x2 = tf.keras.layers.Dense(10)(c2)x3 = tf.keras.layers.Dense(10)(c3)# merged layer y1y1 = tf.keras.layers.Concatenate(axis=1)([x1, x2])# merged layer y2y2 = tf.keras.layers.Concatenate(axis=1)([y1, x3])# print infoprint("-"*30)print("x1", x1.shape, "x2", x2.shape, "x3", x3.shape)print("y1", y1.shape)print("y2", y2.shape)print("-"*30)

Result:

------------------------------x1 (2, 10) x2 (2, 10) x3 (2, 10)y1 (2, 20)y2 (2, 30)------------------------------


You can experiment with model.summary() (notice the concatenate_XX (Concatenate) layer size)

# merge samples, two input must be same shapeinp1 = Input(shape=(10,32))inp2 = Input(shape=(10,32))cc1 = concatenate([inp1, inp2],axis=0) # Merge data must same row columnoutput = Dense(30, activation='relu')(cc1)model = Model(inputs=[inp1, inp2], outputs=output)model.summary()# merge row must same column sizeinp1 = Input(shape=(20,10))inp2 = Input(shape=(32,10))cc1 = concatenate([inp1, inp2],axis=1)output = Dense(30, activation='relu')(cc1)model = Model(inputs=[inp1, inp2], outputs=output)model.summary()# merge column must same row sizeinp1 = Input(shape=(10,20))inp2 = Input(shape=(10,32))cc1 = concatenate([inp1, inp2],axis=1)output = Dense(30, activation='relu')(cc1)model = Model(inputs=[inp1, inp2], outputs=output)model.summary()

You can view notebook here for detail:https://nbviewer.jupyter.org/github/anhhh11/DeepLearning/blob/master/Concanate_two_layer_keras.ipynb