如何在Keras Python中合并多个顺序模型?

我在构建一个模型时,需要将多个顺序模型合并后再对数据集进行训练。似乎keras.engine.topology.Merge在Keras 2.0中已不再支持。我尝试使用keras.layers.Addkeras.layers.Concatenate,但这些方法也行不通。

这是我的代码:

model = Sequential()model1 = Sequential()model1.add(Embedding(len(word_index) + 1, 300, weights = [embedding_matrix], input_length = 40, trainable = False))model1.add(TimeDistributed(Dense(300, activation = 'relu')))model1.add(Lambda(lambda x: K.sum(x, axis = 1), output_shape = (300, )))model2 = Sequential()###与model1相同###model3 = Sequential()model3.add(Embedding(len(word_index) + 1, 300, weights = [embedding_matrix], input_length = 40, trainable = False))model3.add(Convolution1D(nb_filter = nb_filter, filter_length = filter_length, border_mode = 'valid', activation = 'relu', subsample_length = 1))model3.add(GlobalMaxPooling1D())model3.add(Dropout(0.2))model3.add(Dense(300))model3.add(Dropout(0.2))model3.add(BatchNormalization())model4 = Sequential()###与model3相同###model5 = Sequential()model5.add(Embedding(len(word_index) + 1, 300, input_length = 40, dropout = 0.2))model5.add(LSTM(300, dropout_W = 0.2, dropout_U = 0.2))model6 = Sequential()###与model5相同###merged_model = Sequential()merged_model.add(Merge([model1, model2, model3, model4, model5, model6], mode = 'concat'))merged_model.add(BatchNormalization())merged_model.add(Dense(300))merged_model.add(PReLU())merged_model.add(Dropout(0.2))merged_model.add(Dense(1))merged_model.add(BatchNormalization())merged_model.add(Activation('sigmoid'))merged_model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])checkpoint = ModelCheckpoint('weights.h5', monitor = 'val_acc', save_best_only = True, verbose = 2)merged_model.fit([x1, x2, x1, x2, x1, x2], y = y, batch_size = 384, nb_epoch = 200, verbose = 1, validation_split = 0.1, shuffle = True, callbacks = [checkpoint])

错误信息:

name 'Merge' is not defined

使用keras.layers.Addkeras.layers.Concatenate时,提示无法对顺序模型执行此操作。

有什么解决方法吗?


回答:

如果我是你,我会在这个情况下使用Keras函数式API,至少用于创建最终模型(即merged_model)。它提供了更多的灵活性,让你可以轻松定义复杂的模型:

from keras.models import Modelfrom keras.layers import concatenatemerged_layers = concatenate([model1.output, model2.output, model3.output,                             model4.output, model5.output, model6.output])x = BatchNormalization()(merged_layers)x = Dense(300)(x)x = PReLU()(x)x = Dropout(0.2)(x)x = Dense(1)(x)x = BatchNormalization()(x)out = Activation('sigmoid')(x)merged_model = Model([model1.input, model2.input, model3.input,                      model4.input, model5.input, model6.input], [out])merged_model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

你也可以对你定义的其他模型进行同样的操作。正如我提到的,函数式API让你对模型结构有更多的控制,因此在创建像这样的复杂模型时,建议使用它。

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注