Python Keras中的多任务学习

Python Keras中的多任务学习,python,keras,neural-network,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))) in

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

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\u train\u input1,Y\u train\u 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....
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...
有人知道如何在两个输入(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)

但是我试着像这样测试模型

multitask_model.predict_classes(X_test)
我有一个错误:

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

我遗漏了什么?

您的模型只有一个输出,而您正在传递两个:
Y\u train\u input1
Y\u train\u input2

如果您的目标不是合并两个模型,那么您应该将它们分开。当合并/求和输出时,最终只有一个输出

您是否真的打算拥有两个不同的独立模型,而不在它们之间进行任何交互

  • 您有一个公共输出和一个公共的
    Y\u序列
    ,或者
  • 您有两个独立的输出和两个不同的目标
我用这个
y\u pred=model.predict([X\u test,X\u test]).argmax(axis=1)解决了同样的问题。

你必须传递两个数组而不是一个

你可以使用keras functional API()。Hello@Maria,因为你创建了一个需要两个输入的模型,所以它总是需要两个输入(X1和X2)。我不确定你的意图,如果你真的想要两个不同的并行模型来预测一个结果,或者你只是想通过两次使用来加速一个模型。无论如何,您的培训和预测应该遵循相同数量的输入和输出。(除非你构建了更多的模型,包括这些模型的一部分)我认为你应该创建一个详细的问题,明确说明你对每个模型的期望,如果它们相同或不同,为什么你试图预测与培训不同,等等。感谢你的帮助!我会的!哦,我想我明白你的意思了!我想要的是合并层以便有一个单一的输出,但我当前的问题是,我有两个输入数据的不同标签。所以,如果我只想要一个输出,我想我必须修改我的标签,以使Y_train_input1和Y_train_input2相同,对吗?我刚刚遇到另一个问题,我在问题中编辑了它。你知道我如何解决它吗?如果我们有一个共同的输出和一个共同的Y_序列,那么我们将面临另一个问题,那就是训练数据的不同样本,这意味着两个数据集的长度不同,即使它们有相似的类,但类的长度仍然不同。现在这是我的问题:所有输入数组(x)应具有相同数量的样本。获得数组形状:[(24424,15,12),(16325,15,12)]