我一直在尝试使用Keras构建多输入模型。我来自使用顺序模型,只有一个输入是相当直接的。我一直在关注StackOverflow(https://keras.io/getting-started/functional-api-guide/)上的文档(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`.
答案 0 :(得分:4)
错误很明显(而且你几乎就在那里)。代码当前正尝试将输入设置为模型[scatter.plot <- plot_ly(type='scatter',mode="markers",x=~df$x,y=~df$y,data=df) %>%
layout(title="TITLE",xaxis=list(title="X",zeroline=F),yaxis=list(title="Y",zeroline=F))
scatter.plot <- scatter.plot %>% add_trace(scatter.plot,error_y=list(value=df$y.err),data=df,showlegend=F)
],而输入必须是输入图层[left_model, right_model
]。上面代码示例的相关部分应为:
left, right
请在此处参阅我的回答:Merging layers,尤其是第二个示例。