3

So i have written a network which consists of the followings for multi-class classification : -y_labels transformed with to_categorical -last layer uses a sigmoid function with 3 neurons as my classes -model compile uses categorical_crossentropy as loss function So i used

 model.predict_classes(x_test)

and then i used it as

   classification_report(y_test,pred)

y_test has the form to_categorical And i am getting the following error :

ValueError: Mix type of y not allowed, got types set(['binary', 'multilabel-indicator'])

My question is how can i transform it back in order to use it as such?

meme mimis
  • 81
  • 1
  • 6

1 Answers1

5

The error simply indicates that y_test and pred are of different types. Check function type_of_target in multiclass.py. As indicated here one of the y is indicator of classes and another of class vector. You can infer which one is what just by printing the shape, y_test.shape , pred.shape.

More over since you are using model.predict_classes instead of model.predict your output of model.predict_classes will be just classes and not class vector.

So either you need to convert one of them by:

# class --> class vector
from keras.utils import np_utils
x_vec = np_utils.to_categorical(x, nb_classes)

# class vector --> class 
x = x_vec.argmax(axis=-1)
indraforyou
  • 8,969
  • 3
  • 49
  • 40
  • Thanks you very much from your comment. it was just a simple solution although it took me some time. thanks again – meme mimis Jan 12 '17 at 13:07
  • No problem. Can you accept the answer if it helped. – indraforyou Jan 12 '17 at 18:29
  • @indraforyou if you have a chance can you please take a look at this question https://stackoverflow.com/questions/66386561/keras-accuracy-is-different-between-model-predict-model-evaluate-and-classifica/ – Joseph Adam Mar 01 '21 at 09:50