【发布时间】:2019-04-20 12:47:53
【问题描述】:
我有两个不同的数据集,我想尝试多任务学习。我的问题是我能找到的所有示例都有两个不同的训练输入,但标签是相同的。我的问题是:我可以有不同的标签吗?这是我现在的代码:
input1 = Sequential()
input1.add(Embedding(vocabulary_size, embedding_size,
input_length=longest_sen_input1))
input1.add(Bidirectional(LSTM(units=embedding_size)))
input1.add(Dense(len(document), activation='softmax'))
input2 = Sequential()
input2.add(Embedding(vocabulary_size, embedding_size,
input_length=longest_sen_input2))
input2.add(Bidirectional(LSTM(units=embedding_size)))
input2.add(Dense(len(document), activation='softmax'))
model = Sequential()
model.add(Merge([input1, input2], mode='sum'))
model.add(Dense(len(document), activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam')
model.fit([X_train_input1, X_train_input2], [Y_train_input1, Y_train_input2], epochs=100)
我尝试插入 [Y_train_input1, Y_train_input2],但出现此错误:
Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: [array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 0., 0....
有人知道如何使用两个输入(X_train_input1/Y_train_input1 和 X_train_input2/Y_train_input2)执行多任务学习,返回一个共同的预测?
编辑 我的模型现在似乎可以工作了,我只是改变了
model.fit([X_train_input1, X_train_input2], [Y_train_input1, Y_train_input2], epochs=100)
在
model.fit([X_train_input1, X_train_input2], Y_train, epochs=100)
然后我尝试像这样测试模型
multitask_model.predict_classes(X_test)
我有这个错误:
ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[ 0, 0, 0, ..., 13, 8, 134],
[ 0, 0, 0, ..., 33, 87, 19],
[ 0, 0, 0, ..., 27, 1, 4],
...,
[ 0, 0, 0, ..., 1, 10, 8],
[ 0...
我错过了什么?
【问题讨论】:
-
您可以使用 keras 函数式 API (keras.io/getting-started/functional-api-guide)。
-
你好@Maria,因为你创建了一个接受两个输入的模型,它总是接受两个输入(X1 和 X2)。我不确定你的意图,如果你真的想要两个不同的并行模型来预测一个结果,或者你可能试图通过使用两次来简单地加速一个模型。无论如何,您的训练和预测应该遵循相同数量的输入和输出。 (除非您构建更多模型来获取这些模型的一部分)
-
我认为也许您应该创建一个详细的问题,准确说明您对每个模型的期望,它们是相同还是不同,为什么您尝试与训练进行不同的预测等等。跨度>
-
感谢您的帮助!我会这样做的!
标签: python keras neural-network