我的神经网络预测出现错误:IndexError: list index out of range

我正在进行一个简单的垃圾邮件/非垃圾邮件文本分类。我的Keras神经网络训练和评估都正常进行;然而,当我尝试预测如下格式的新文本时,会得到“IndexError: list index out of range”错误:

model.predict(cleaning_funcs('my bus departs in five minutes'))

如果有帮助的话,我还使用了以下代码:

from keras.preprocessing.text import Tokenizertokenizer = Tokenizer(num_words=5000)tokenizer.fit_on_texts(x_train)x_train = tokenizer.texts_to_sequences(x_train)x_test = tokenizer.texts_to_sequences(x_test)vocab_size = len(tokenizer.word_index) + 1print(x_train[2])from keras.preprocessing.sequence import pad_sequencesmaxlen = 100x_train = pad_sequences(x_train, padding='post', maxlen=maxlen)x_test = pad_sequences(x_test, padding='post', maxlen=maxlen)

回答:

我猜你的cleaning_funcs函数没有返回一个数组,而predict函数需要一个数组,你可以尝试这样做:

model.predict([cleaning_funcs('my bus departs in five minutes')])

更多信息请查看 https://www.tensorflow.org/api_docs/python/tf/keras/Model#predict

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中创建了一个多类分类项目。该项目可以对…

发表回复

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