目前正在处理一个深度学习示例,他们正在使用Tokenizer包。我收到以下错误:
AttributeError:“Tokenizer”对象没有属性“”word_index“”
下面是我的代码:
from keras.preprocessing.text import Tokenizer
samples = ['The cat say on the mat.', 'The dog ate my homework.']
tokenizer = Tokenizer(num_words=1000)
tokenizer.fit_on_sequences(samples)
sequences = tokenizer.texts_to_sequences(samples)
one_hot_results = tokenizer.texts_to_matrix(samples, mode='binary')
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))有人能帮我找出我的错误吗?
发布于 2018-02-03 01:25:37
它似乎正在正确导入,但Tokenizer对象没有属性word_index。
根据documentation,只有在Tokenizer对象上调用fits_on_text方法时,才会设置该属性。
以下代码成功运行:
from keras.preprocessing.text import Tokenizer
samples = ['The cat say on the mat.', 'The dog ate my homework.']
tokenizer = Tokenizer(num_words=1000)
tokenizer.fit_on_texts(samples)
one_hot_results = tokenizer.texts_to_matrix(samples, mode='binary')
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))https://stackoverflow.com/questions/48587696
复制相似问题