Multitask learning in Keras

Viewed 1323

I have two different datasets and I would like to try multi-task learning. My problem is that all the examples I could find have two different training inputs, but the labels are the same. My question is: can I have different labels? This is my code right now:

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)

I try to insert [Y_train_input1, Y_train_input2], but I have this error:

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

Does anybody know how to perform multitask learning with two inputs (X_train_input1/Y_train_input1 and X_train_input2/Y_train_input2) returning a common prediction?

EDIT My model seems to work now, I simply changed

model.fit([X_train_input1, X_train_input2], [Y_train_input1, Y_train_input2], epochs=100)

in

model.fit([X_train_input1, X_train_input2], Y_train, epochs=100)

but then I try to test the model like this

multitask_model.predict_classes(X_test)

and I have this error:

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

What am I missing?

2 Answers

Your model has only one output, and you're passing two: Y_train_input1, Y_train_input2.

If your goal is not to merge both models then you should keep them separate. When you merge/sum the outputs, you end up with only one output.

Is your intention to really have two different separate models without any interaction between them?

  • Either you have a common output and a common Y_train, or
  • You have two separate outputs and two different targets.

I solved the same problem using this y_pred = model.predict([X_test,X_test]).argmax(axis=1) you have to pass two arrays instead of one

Related