在Keras中使用预测功能时发生“ KeyError”

时间:2019-05-10 07:17:01

标签: python tensorflow keras prediction natural-language-processing

我是机器学习的初学者。我在网上找到了以下代码,用于使用imdb数据集进行情感分析。

使用此数据集无法成功创建经过训练的模型。在预测阶段,当我尝试对此模型进行预测时,提交的不在训练集中的字符串会得到一些“ 键错误”响应。

问题出在哪里?该模型只能预测数据集中存在的单词的情感吗?非常感谢您的帮助。

from keras.preprocessing.text import text_to_word_sequence

review = "i love stackoverflow"
text = text_to_word_sequence(review)
tmp = []

for word in text:
    tmp.append(word_to_id[word])
    tmp_padded = sequence.pad_sequences([tmp], maxlen=max_review_length)

print("Sentiment score: %s" % (model.predict(array([tmp_padded][0]))[0][0]))
  

KeyError跟踪(最近一次通话最后一次)

     

KeyError:'stackoverflow'

代码参考:https://www.liip.ch/en/blog/sentiment-detection-with-keras-word-embeddings-and-lstm-deep-learning-networks

0 个答案:

没有答案
相关问题