无法从Keras导入Tokenizer

目前正在研究一个深度学习示例,他们使用了Tokenizer包。我遇到了以下错误:

AttributeError: ‘Tokenizer’ 对象没有属性 ‘word_index’

这是我的代码:

from keras.preprocessing.text import Tokenizersamples = ['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_indexprint('Found %s unique tokens.' % len(word_index))

有谁能帮我找出我的错误吗?


回答:

看起来导入是正确的,但是Tokenizer对象没有word_index属性。

根据文档,该属性只有在对Tokenizer对象调用fit_on_texts方法后才会设置。

以下代码可以成功运行:

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))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注