Python 如何从 Keras.layers 实现 Merge

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51075666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:42:55  来源:igfitidea点击:

How to implement Merge from Keras.layers

pythonpython-3.xneural-networkkeraskeras-layer

提问by Raj Dayal

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"

代码在使用“merge”时编译但给出以下错误“TypeError:'module' object is not callable”但是它在使用“Merge”时甚至无法编译

I am using keras version 2.2.0 and python 3.6

我正在使用 keras 2.2.0 版和 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

采纳答案by Daniel GL

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.

合并不能与顺序模型一起使用。在顺序模型中,层只能有一个输入和一个输出。您必须使用功能 API,就像这样。我假设您对 modela 和 modelb 使用相同的输入层,但如果不是这种情况,您可以创建另一个 Input() 并将它们都作为模型的输入。

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

回答by Quantum

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

keras.layers.merge 层已弃用。使用keras.layers.Concatenate(axis=-1)这里提到的:https: //keras.io/layers/merge/#concatenate

回答by Castiel Wong

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:

幸运的是,我终于找到了预期的灵丹妙药。对于想要使用Sequential对其原始代码进行最小更改的任何人,这里有解决方案:

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-335457553for farizrahman4u's comment. ;)

欲了解更多信息,请参阅https://github.com/keras-team/keras/issues/3921#issuecomment-335457553farizrahman4u“的评论。;)