python - 使用 Keras 的多输入模型(模型 API)

标签 python keras

我一直在尝试使用 Keras 构建多输入模型。我来自使用顺序模型并且只有一个相当简单的输入。我一直在查看文档 ( https://keras.io/getting-started/functional-api-guide/ ) 和 StackOverflow 上的一些答案 ( How to "Merge" Sequential models in Keras 2.0? )。基本上我想要的是让两个输入训练一个模型。一个输入是一段文本,另一个是从该文本中提取的一组精选特征。手工挑选的特征向量具有恒定长度。以下是我到目前为止尝试过的内容:

    left = Input(shape=(7801,), dtype='float32', name='left_input')
    left = Embedding(7801, self.embedding_vector_length, weights=[self.embeddings],
                     input_length=self.max_document_length, trainable=False)(left)

    right = Input(shape=(len(self.z_train), len(self.z_train[0])), dtype='float32', name='right_input')

    for i, filter_len in enumerate(filter_sizes):
        left = Conv1D(filters=128, kernel_size=filter_len, padding='same', activation=c_activation)(left)
        left = MaxPooling1D(pool_size=2)(left)

    left = CuDNNLSTM(100, unit_forget_bias=1)(left)
    right = CuDNNLSTM(100, unit_forget_bias=1)(right)

    left_out = Dense(3, activation=activation, kernel_regularizer=l2(l_2), activity_regularizer=l1(l_1))(left)
    right_out = Dense(3, activation=activation, kernel_regularizer=l2(l_2), activity_regularizer=l1(l_1))(right)
    for i in range(self.num_outputs):
        left_out = Dense(3, activation=activation, kernel_regularizer=l2(l_2), activity_regularizer=l1(l_1))(left_out)
        right_out = Dense(3, activation=activation, kernel_regularizer=l2(l_2), activity_regularizer=l1(l_1))(right_out)

    left_model = Model(left, left_out)
    right_model = Model(right, right_out)

    concatenated = merge([left_model, right_model], mode="concat")
    out = Dense(3, activation=activation, kernel_regularizer=l2(l_2), activity_regularizer=l1(l_1), name='output_layer')(concatenated)

    self.model = Model([left_model, right_model], out)
    self.model.compile(loss=loss, optimizer=optimizer, metrics=[cosine, mse, categorical_accuracy])

这给出了错误:

TypeError: Input layers to a `Model` must be `InputLayer` objects. Received inputs: Tensor("cu_dnnlstm_1/strided_slice_16:0", shape=(?, 100), dtype=float32). Input 0 (0-based) originates from layer type `CuDNNLSTM`.

最佳答案

错误很明显(而且您几乎就在那里)。该代码目前正在尝试将输入设置为模型 [left_model, right_model],而输入必须是输入层 [left, right]。上面代码示例的相关部分应为:

self.model = Model([left, rigt], out)

在这里查看我的回答作为引用:Merging layers特别是第二个例子。

关于python - 使用 Keras 的多输入模型(模型 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47947578/

相关文章:

python - RuntimeWarning : numpy. dtype 大小已更改,可能表示二进制不兼容

python - 在嵌套数组 pymongo 中查找

python - 具有大数据集的 Keras 模型的超参数优化

python - 无法挤压 dim[1],期望维度为 1,得到 499

python - 如何通过操作更改 Tkinter 中框的背景颜色

python - 为什么 .loc 对切片具有包容性行为?

python - 检查表的特定列在 python 中是否为空,其中整个表内容存储在一个变量中

python - Keras 模型 LSTM 预测 2 个特征

python - 如何在Keras中获取预测错误数据的索引?

tensorflow - Keras 使用预训练嵌入初始化大型嵌入层