how to wrap a multi-output Keras model in a wrappers.scikit_learn.KerasClassifier?

Viewed 182

I can cross-validate my keras model using KerasClassifier sklearn wrapper.

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score

estimator = KerasClassifier(build_fn=get_model())

results = cross_val_score(estimator, X_train, a_train)

However, this fails when I have a multi-output model e.g.:

def get_multioutput_model():
[...]
out1= layers.Dense(5, activation="softmax",name='A')(x)
out2= layers.Dense(7, activation="softmax",name='B')(x)
model = tf.keras.Model(inputs=inputs, outputs=[out1,out2])
[...]

I can fit the model correctly by

model.fit(X_train,{'A':a_train,'B':b_train})

However the following fails

estimator = KerasClassifier(build_fn=get_multioutput_model())

results = cross_val_score(estimator, X_train, {'A':a_train,'B':b_train})

ValueError: Found input variables with inconsistent numbers of samples: [22658, 2]

How to achieve the same behavior as for the single-output model?

0 Answers
Related