ValueError: Cannot handle a combination of multivalued indicator and binary

I am using Keras with the scikit-learn cover. Specifically, I want to use GridSearchCV to optimize hyperparameters.

This is a multi-class issue, that is, the target variable can only have one label selected in a set of n classes. For example, the target variable could be "Class1", "Class2" ... "Classn".

# self._arch creates my model
nn = KerasClassifier(build_fn=self._arch, verbose=0)
clf = GridSearchCV(
  nn,
  param_grid={ ... },
  # I use f1 score macro averaged
  scoring='f1_macro',
  n_jobs=-1)

# self.fX is the data matrix
# self.fy_enc is the target variable encoded with one-hot format
clf.fit(self.fX.values, self.fy_enc.values)

      

The problem is that when the score is computed in cross-validation, the true label for the validation samples is encoded one-time, and the prediction collapses to a binary label for some reason (when the target variable has only two classes). For example, this is the last part of the stack trace:

...........................................................................
/Users/fbrundu/.pyenv/versions/3.6.0/lib/python3.6/site-packages/sklearn/metrics/classification.py in _check_targets(y_true=array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0... 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.]]), y_pred=array([1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1,...0, 1, 0, 0, 1, 0, 0, 0,
       0, 0, 0, 0, 1, 1]))
     77     if y_type == set(["binary", "multiclass"]):
     78         y_type = set(["multiclass"])
     79
     80     if len(y_type) > 1:
     81         raise ValueError("Can't handle mix of {0} and {1}"
---> 82                          "".format(type_true, type_pred))
        type_true = 'multilabel-indicator'
        type_pred = 'binary'
     83
     84     # We can't have more than one value on y_type => The set is no more needed
     85     y_type = y_type.pop()
     86

ValueError: Can't handle mix of multilabel-indicator and binary

      

How can I instruct Keras / sklearn to return predictions in one hot coding?

+3


source to share


1 answer


Following on from Vivek's comment, I used the original (not one hot) target array and I configured (in my Keras model, see code) loss sparse_categorical_crossentropy

, as per the comment on this issue .



arch.compile(
  optimizer='sgd',
  loss='sparse_categorical_crossentropy',
  metrics=['accuracy'])

      

+2


source







All Articles