我使用 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)
但是当我尝试进行预测时,我得到了以下回溯的错误
Traceback (most recent call last):
File "", line 1, in runfile('C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py', wdir='C:/Users/Radosław/nauka/python/SentimentAnalysis')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py", line 47, in prediction = model.predict(testArray)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\models.py", line 899, in predict return self.model.predict(x, batch_size=batch_size, verbose=verbose)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1555, in predict check_batch_axis=False)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 133, in _standardize_input_data str(array.shape))
ValueError: Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3)
是否有适当的方法来 reshape 我的输入数组?
最佳答案
你什么都做了,但忘记了序列填充。在调用预测之前添加此行。
testArray = sequence.pad_sequences(testArray, maxlen=maxlen)
关于python - Keras 检查 : expected embedding_1_input to have shape (None, 100 时出错,但得到形状为 (1, 3) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43490807/