我有两个不同的数据集,我想尝试多任务学习。我的问题是,我可以找到的所有示例都有两个不同的训练输入,但是标签是相同的。我的问题是:我可以有不同的标签吗?这是我现在的代码:
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...
我想念什么?
最佳答案
您的模型只有一个输出,您要传递两个输出:Y_train_input1
,Y_train_input2
。
如果您的目标不是合并两个模型,则应将它们分开。合并/求和输出时,最终只会得到一个输出。
您是否打算真正拥有两个不同的单独模型,并且它们之间没有任何相互作用?
Y_train
,或者关于python - Keras中的多任务学习,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53352954/