您的示例是胡言乱语的,它比仅缺少一些停用词要糟糕得多。
如果重新阅读了的文档
start_char,
oov_char以及
index_from在[参数
keras.datasets.imdb.load_data](https://keras.io/datasets/#imdb-
movie-reviews-sentiment-classification 他们解释发生了什么事)方法:
start_char:int 序列的开始将用此字符标记。设置为1是因为0通常是填充字符。
oov_char:int 由于num_words或skip_top限制而被切掉的单词将被替换为该字符。
index_from:int 使用此索引和更高的索引来索引实际单词。
您倒置的那本词典假定单词索引从开始
1。
但是索引返回了我的喀拉拉邦(Keras)
<START>和
<UNKNOWN>作为索引
1和
2。(它假定你将使用
0的
<PADDING>)。
这对我有用:
import kerasNUM_WORDS=1000 # only use top 1000 wordsINDEX_FROM=3 # word index offsettrain,test = keras.datasets.imdb.load_data(num_words=NUM_WORDS, index_from=INDEX_FROM)train_x,train_y = traintest_x,test_y = testword_to_id = keras.datasets.imdb.get_word_index()word_to_id = {k:(v+INDEX_FROM) for k,v in word_to_id.items()}word_to_id["<PAD>"] = 0word_to_id["<START>"] = 1word_to_id["<UNK>"] = 2word_to_id["<UNUSED>"] = 3id_to_word = {value:key for key,value in word_to_id.items()}print(' '.join(id_to_word[id] for id in train_x[0] ))标点符号丢失了,仅此而已:
"<START> this film was just brilliant casting <UNK> <UNK> story direction <UNK> really <UNK> the part they played and you could just imagine being there robert <UNK> is an amazing actor ..."



