Keras中的多任务学习

时间:2018-11-17 16:04:47

标签: python keras neural-network

我有两个不同的数据集,我想尝试多任务学习。我的问题是我可以找到的所有示例都有两个不同的训练输入,但是标签是相同的。我的问题是:我可以有不同的标签吗?这是我现在的代码:

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...

我想念什么?

2 个答案:

答案 0 :(得分:0)

您的模型只有一个输出,您要传递两个输出:Y_train_input1Y_train_input2

如果您的目标不是合并两个模型,则应将它们分开。合并/求和输出时,最终只会得到一个输出。

您是否打算真正拥有两个不同的单独模型而它们之间没有任何交互?

  • 您有一个通用的输出和一个通用的Y_train,或者
  • 您有两个单独的输出和两个不同的目标。

答案 1 :(得分:0)

我使用此y_pred = model.predict([X_test,X_test]).argmax(axis=1)解决了相同的问题 您必须传递两个数组而不是一个

相关问题