我一直在尝试合并以下顺序模型,但是无法合并。有人可以指出我的错误,谢谢。
使用“合并”时代码会编译,但会出现以下错误“ TypeError:'模块'对象不可调用” 但是,即使在使用“合并”时也无法编译
我正在使用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
答案 0 :(得分:4)
合并不能与顺序模型一起使用。在顺序模型中,图层只能有一个输入和一个输出。 您必须使用functional 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
答案 1 :(得分:3)
不推荐使用keras.layers.merge层。如此处所述,使用keras.layers.Concatenate(axis=-1)
代替:https://keras.io/layers/merge/#concatenate
答案 2 :(得分:1)
说实话,我在这个问题上苦苦挣扎了很长时间……
幸运的是我终于找到了万能药。对于任何想使用 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
有关更多信息,请参考https://github.com/keras-team/keras/issues/3921#issuecomment-335457553以获取farizrahman4u
的评论。 ;)