应该在至少2个输入的列表上调用“连接”层

时间:2018-10-15 08:17:29

标签: python machine-learning keras artificial-intelligence

我试图在Keras中为CNN深度学习创建两个顺序模型,并在将其添加到密集层之前合并(合并)这两个模型。但是我得到了错误:     应该在至少2个输入的列表上调用Concatenate

    model_1 = models.Sequential()
    model_1.add(layers.Conv1D(num_filters, 7, activation='relu',
        input_shape=(TEXT_MAX_LENGTH, LENGTH_ALPHABET)))
    model_1.add(layers.MaxPooling1D(pool_size=3))
    model_1.add(layers.Flatten())

    model_2 = models.Sequential()
    model_2.add(layers.Conv1D(num_filters, 7, activation='relu',
        input_shape=(TEXT_MAX_LENGTH, LENGTH_ALPHABET)))
    model_2.add(layers.Conv1D(num_filters, 7, activation='relu'))
    model_2.add(layers.MaxPooling1D(3))
    model_2.add(layers.Flatten())

    concat = Concatenate([model_1, model_2]) 

    merged_model = models.Sequential()      

    model.add(concat)


    model.add(layers.Dense(width_hidden, activation='relu'))
    model.add(layers.Dropout(rate=dropout))
    model.add(layers.Dense(width_output, activation='softmax'))

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

    model.fit(x_train, y_train,
       batch_size=batch_size,
       epochs=epochs,
       verbose=1,
       callbacks=callbacks_list,
       validation_data=(x_test, y_test)
       )

1 个答案:

答案 0 :(得分:0)

使用concatenate小写字母。

from keras.layers import concatenate

并使用模型的输出进行连接:

concat = concatenate([model_1.output, model_2.output])

或者另一种方法是使用keras的 Functional API