我正在进行一个简单的垃圾邮件/非垃圾邮件文本分类。我的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