5

How do I train 1 model multiple times and combine them at the output layer?

For example:

model_one = Sequential() #model 1
model_one.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
model_one.add(Flatten())
model_one.add(Dense(128, activation='relu'))

model_two = Sequential() #model 2
model_two.add(Dense(128, activation='relu', input_shape=(784)))
model_two.add(Dense(128, activation='relu'))

model_???.add(Dense(10, activation='softmax')) #combine them here

model.compile(loss='categorical_crossentropy', #continu together
          optimizer='adam',
          metrics=['accuracy'])


model.fit(X_train, Y_train, #continu together somehow, even though this would never work because X_train and Y_train have wrong formats
      batch_size=32, nb_epoch=10, verbose=1)

I've heard I can do this through a graph model but I can't find any documentation on it.

EDIT: in reply to the suggestion below:

A1 = Conv2D(20,kernel_size=(5,5),activation='relu',input_shape=( 28, 28, 1))
---> B1 = MaxPooling2D(pool_size=(2,2))(A1)

throws this error:

AttributeError: 'Conv2D' object has no attribute 'get_shape'
Perm. Questiin
  • 429
  • 2
  • 9
  • 21

1 Answers1

5

Graph notation would do it for you. Essentially you give every layer a unique handle then link back to the previous layer using the handle in brackets at the end:

layer_handle = Layer(params)(prev_layer_handle)

Note that the first layer must be an Input(shape=(x,y)) with no prior connection.

Then when you make your model you need to tell it that it expects multiple inputs with a list:

model = Model(inputs=[in_layer1, in_layer2, ..], outputs=[out_layer1, out_layer2, ..])

Finally when you train it you also need to provide a list of input and output data that corresponds with your definition:

model.fit([x_train1, x_train2, ..], [y_train1, y_train2, ..])

Meanwhile everything else is the same so you just need to combine together the above to give you the network layout that you want:

from keras.models import Model
from keras.layers import Input, Convolution2D, Flatten, Dense, Concatenate

# Note Keras 2.02, channel last dimension ordering

# Model 1
in1 = Input(shape=(28,28,1))
model_one_conv_1 = Convolution2D(32, (3, 3), activation='relu')(in1)
model_one_flat_1 = Flatten()(model_one_conv_1)
model_one_dense_1 = Dense(128, activation='relu')(model_one_flat_1)

# Model 2
in2 = Input(shape=(784, ))
model_two_dense_1 = Dense(128, activation='relu')(in2)
model_two_dense_2 = Dense(128, activation='relu')(model_two_dense_1)

# Model Final
model_final_concat = Concatenate(axis=-1)([model_one_dense_1, model_two_dense_2])
model_final_dense_1 = Dense(10, activation='softmax')(model_final_concat)

model = Model(inputs=[in1, in2], outputs=model_final_dense_1)

model.compile(loss='categorical_crossentropy', #continu together
              optimizer='adam',
              metrics=['accuracy'])

model.fit([X_train_one, X_train_two], Y_train,
          batch_size=32, nb_epoch=10, verbose=1)

Documentation can be found in the Functional Model API. I'd recommend reading around other questions or checking out Keras' repo as well since the documentation currently doesn't have many examples.

Lif3line
  • 111
  • 6
  • Thank you so much! Where do you import Model and Concatenate from? Do you have a link to the right documentation? – Perm. Questiin Jul 02 '17 at 19:56
  • It goes wrong at the second line. I edited the error I get. – Perm. Questiin Jul 02 '17 at 20:20
  • 1
    No problem, should work now, I mixed up the two APIs, you need an explicit Input() layer in the functional version. I think you may also be using an older version of Keras; I've updated for v2 and the default channel last dimension ordering which you may have to change on your end. – Lif3line Jul 03 '17 at 08:12
  • I'll look at it this week. I don't have time right now. Thank you for the update! – Perm. Questiin Jul 03 '17 at 15:55
  • https://stackoverflow.com/questions/68330534/merge-multiple-models-in-keras-tensorflow can you help me with this? – Coder Jul 10 '21 at 21:39