Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use to_categorical in keras

Tags:

keras

Please someone explain when keras.utils.np_utils.to_categorical is to be used?
I understand that it converts class vector to binary matrix, probably used in Deep Learning model.
But if we still go ahead and use the class vector itself, and probably use model.predict_classes - what is the drawback?

like image 723
Neeraj G Avatar asked May 22 '17 10:05

Neeraj G


2 Answers

A classification model with multiple classes doesn't work well if you don't have classes distributed in a binary matrix.

Suppose you have three clasess, the vector goes like this:

  • [1, 0, 0] = class 1
  • [0, 1, 0] = class 2
  • [0, 0, 1] = class 3

You use to_categorical to transform your training data before you pass it to your model.

If your training data uses classes as numbers, to_categorical will transform those numbers in proper vectors for using with models. You can't simply train a classification model without that.

Unfortunately, predict_classes is not documented, so it's probably better not to use it. But I suppose it does exactly the inverse operation to_categorical does. Your model outputs the vectors, and predict_classes transforms those vectors in human readable classes.

like image 138
Daniel Möller Avatar answered Sep 20 '22 22:09

Daniel Möller


I know this is an old thread, but figured I'd help clarify.

The reason you want to_categorical (even on numeric labels) is due to how the relationship between your labels is understood by the algorithm.

For example, suppose you made a color classifier. You mark red as 1, blue as 2, and orange as 3.

Now you feed them into the machine learning algorithm to help decide what your input matches. The math is going to say that orange is higher than red. This obviously isn't your intent, but the network will know that orange is greater than red.

like image 23
Cody Avatar answered Sep 18 '22 22:09

Cody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!