NG的视频看完后,想着进行实战于是乎最近把Keras之父写的书籍看了一遍,做个入门的记录吧。
one-hot编码:
from keras.preprocessing.text import Tokenizer
samples = ['The cat sat on the mat.' , 'The dog ate my homework.']
tokenizer = Tokenizer(num_words=100) #创建一个分词器,设置为只考虑前10000个常见词
tokenizer.fit_on_texts(samples)#构建单词索引
sequence = tokenizer.texts_to_matrix(samples,mode = 'binary')#将字符串转换为整数索引列表
word_index = tokenizer.word_index#找回索引
print(len(word_index))
print(word_index)