4

我使用 imdb 示例创建了 LSTM 模型,并尝试在我自己的字符串中预测情绪

max_features = 20000
# cut texts after this number of words
# (among top max_features most common words)
maxlen = 100
batch_size = 32


wordsA = "I like this review"

wordIndexes = imdb.get_word_index()

wordArray = wordsA.split()
intArray = []
for word in wordArray:
    if word in wordIndexes:
        intArray.append(wordIndexes[word])

testArray = np.array([intArray])

print('Shape: '+str(testArray.shape)) 

model = load_model('my_model2.h5')

print(str(testArray))

prediction = model.predict(testArray)
print(prediction)        

但是当我尝试进行预测时,我会因以下回溯而出错

回溯(最近一次通话最后):

文件“”,第 1 行,在运行文件中('C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py', wdir='C:/Users/Radosław/nauka/python/SentimentAnalysis')

文件“C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”,第 866 行,运行文件 execfile(文件名,命名空间)

文件“C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py”,第 102 行,在 execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件“C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py”,第 47 行,预测 = model.predict(testArray)

文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\models.py”,第 899 行,预测返回 self.model.predict(x, batch_size=batch_size, verbose=verbose)

文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py”,第 1555 行,预测 check_batch_axis=False)

文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py”,第 133 行,在 _standardize_input_data str(array.shape))

ValueError:检查时出错:预期 embedding_1_input 的形状为 (None, 100) 但得到的数组的形状为 (1, 3)

是否有适当的方法来重塑我的输入数组?

4

1 回答 1

1

您做了所有事情,但忘记了序列填充。在调用 predict 之前添加此行。

testArray = sequence.pad_sequences(testArray, maxlen=maxlen)
于 2017-09-05T13:35:31.957 回答