Home > Back-end >  How can keras.utils.to_categorical() support more than one value?
How can keras.utils.to_categorical() support more than one value?

Time:04-24

I know that keras.utils.to_categorical() can be used for one-hot encoding, as in the exmaple of the transformation 2 -> [0., 0., 1., 0.] but is it possible to have an output similar to this? 2, 3 -> [0., 0., 1., 1.] And if so, how please?

CodePudding user response:

You can do this using the following:

layer = tf.keras.layers.CategoryEncoding(output_mode="multi_hot", num_tokens=4)

[nav] In [50]: layer([[2,3]])                                                                                                                                               
Out[50]: <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[0., 0., 1., 1.]], dtype=float32)>   

tf.keras.utils.to_categorical is used in the process to calculate categorical cross entropy, a loss function for binary classification. Also note that you'll need to figure out the number of tokens you have, here I am assuming 4 to cover your [2, 3] scenario. In this case, the encoder can encode [0, 1, 2, 3], you can pass it samples of any length, it will encode them to [0|1, 0|1, 0|1, 0|1].

  • Related