Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Merge from Keras.layers

I have been trying to merge the following sequential models but haven't been able to. Could somebody please point out my mistake, thank you.

The code compiles while using"merge" but give the following error "TypeError: 'module' object is not callable" However it doesn't even compile while using "Merge"

I am using keras version 2.2.0 and python 3.6

from keras.layers import merge
def linear_model_combined(optimizer='Adadelta'):    
    modela = Sequential()
    modela.add(Flatten(input_shape=(100, 34)))
    modela.add(Dense(1024))
    modela.add(Activation('relu'))
    modela.add(Dense(512))

    modelb = Sequential()
    modelb.add(Flatten(input_shape=(100, 34)))
    modelb.add(Dense(1024))
    modelb.add(Activation('relu'))
    modelb.add(Dense(512))

    model_combined = Sequential()

    model_combined.add(Merge([modela, modelb], mode='concat'))

    model_combined.add(Activation('relu'))
    model_combined.add(Dense(256))
    model_combined.add(Activation('relu'))

    model_combined.add(Dense(4))
    model_combined.add(Activation('softmax'))

    model_combined.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    return model_combined
like image 801
Raj Dayal Avatar asked Jun 28 '18 06:06

Raj Dayal


People also ask

How do you merge layers after?

The fastest way is using the keyboard shortcuts: Ctrl+E to merge selected layers, Shift+Ctrl+E to merge all layers.

What is concatenation layer?

A concatenation layer takes inputs and concatenates them along a specified dimension. The inputs must have the same size in all dimensions except the concatenation dimension. Specify the number of inputs to the layer when you create it. The inputs have the names 'in1','in2',...,'inN' , where N is the number of inputs.

What is the difference between concatenate and add in keras?

Add layer adds two input tensor while concatenate appends two tensors.


3 Answers

Merge cannot be used with a sequential model. In a sequential model, layers can only have one input and one output. You have to use the functional API, something like this. I assumed you use the same input layer for modela and modelb, but you could create another Input() if it is not the case and give both of them as input to the model.

def linear_model_combined(optimizer='Adadelta'):    

    # declare input
    inlayer =Input(shape=(100, 34))
    flatten = Flatten()(inlayer)

    modela = Dense(1024)(flatten)
    modela = Activation('relu')(modela)
    modela = Dense(512)(modela)

    modelb = Dense(1024)(flatten)
    modelb = Activation('relu')(modelb)
    modelb = Dense(512)(modelb)

    model_concat = concatenate([modela, modelb])


    model_concat = Activation('relu')(model_concat)
    model_concat = Dense(256)(model_concat)
    model_concat = Activation('relu')(model_concat)

    model_concat = Dense(4)(model_concat)
    model_concat = Activation('softmax')(model_concat)

    model_combined = Model(inputs=inlayer,outputs=model_concat)

    model_combined.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    return model_combined
like image 194
Daniel GL Avatar answered Nov 14 '22 21:11

Daniel GL


The keras.layers.merge layer is deprecated. Use keras.layers.Concatenate(axis=-1) instead as mentioned here: https://keras.io/layers/merge/#concatenate

like image 37
Quantum Avatar answered Nov 14 '22 22:11

Quantum


To be honest, I was struggling on this issue for a long time...

Luckily I found the panacea expected finally. For anyone who would like to make the minimal changes on their original codes with Sequential, here comes the solution:

def linear_model_combined(optimizer='Adadelta'): 
    from keras.models import Model, Sequential
    from keras.layers.core import Dense, Flatten, Activation, Dropout
    from keras.layers import add

    modela = Sequential()
    modela.add(Flatten(input_shape=(100, 34)))
    modela.add(Dense(1024))
    modela.add(Activation('relu'))
    modela.add(Dense(512))

    modelb = Sequential()
    modelb.add(Flatten(input_shape=(100, 34)))
    modelb.add(Dense(1024))
    modelb.add(Activation('relu'))
    modelb.add(Dense(512))

    merged_output = add([modela.output, modelb.output])   

    model_combined = Sequential()
    model_combined.add(Activation('relu'))
    model_combined.add(Dense(256))
    model_combined.add(Activation('relu'))
    model_combined.add(Dense(4))
    model_combined.add(Activation('softmax'))

    final_model = Model([modela.input, modelb.input], model_combined(merged_output))

    final_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    return final_model

For more information, refer to https://github.com/keras-team/keras/issues/3921#issuecomment-335457553 for farizrahman4u's comment. ;)

like image 27
Castiel Wong Avatar answered Nov 14 '22 23:11

Castiel Wong