I am reading Chollet's book on deep learning at the moment and in the NLP chapter he says:

Note that Keras has built-in utilities for doing one-hot encoding of text at the word level or character

I have looked into Keras metods and I cannot find which function he is referring to. keras.utils.to_categorical does not seem to be applicable directly here, as it requires int inputs.

I would like to efficiently encode some text at character level for a small RNN project: what can I use there?


1 Answer 1


I think that you are looking for the keras Tokenizer with the char_level=True flag:

from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(char_level=True)
sequence_of_int = tokenizer.texts_to_sequences(your_dataset_train_or_test)

Now that you have sequences of Integer, you can use keras.utils.to_categorical =)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.