【问题标题】:My neural networks predict is giving me an error: IndexError: list index out of range我的神经网络预测给了我一个错误:IndexError: list index out of range
【发布时间】:2020-11-09 20:58:38
【问题描述】:

我正在做一个简单的垃圾邮件/垃圾邮件文本分类。我的 Keras NN 进行了适当的训练和评估;但是,当我尝试预测以下格式的新文本时,出现“IndexError: list index out of range”错误:

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

如果有帮助,我还使用了以下内容:

from keras.preprocessing.text import Tokenizer
tokenizer = 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) + 1
print(x_train[2])

from keras.preprocessing.sequence import pad_sequences
maxlen = 100
x_train = pad_sequences(x_train, padding='post', maxlen=maxlen)
x_test = pad_sequences(x_test, padding='post', maxlen=maxlen)

【问题讨论】:

    标签: python machine-learning keras neural-network data-analysis


    【解决方案1】:

    我假设您的cleaning_funcs 不返回数组,预测函数期望数组尝试

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

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

    【讨论】:

    • 谢谢!你确实为我指明了正确的方向。干杯
    • 如果此答案不完全符合您的问题,您可以发布自己的正确答案并将其标记为帮助社区的解决方案。
    • 如果我这样做似乎可以工作:model.predict(pad_sequences(tokenizer.texts_to_sequences([cleaning_funcs('我的巴士在五分钟后出发')]), padding='post', maxlen=maxlen) ).round()
    • 嗯,我明白了!我认为cleaning_funcs 正在做这种处理。如果它适合你,那就完美了!
    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多