python - Keras 检查 : expected embedding_1_input to have shape (None, 100 时出错,但得到形状为 (1, 3) 的数组

标签 python keras imdbpy

我使用 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/

相关文章:

python - 在 Image 模块 Python 中淡出

python - 如何使用 Python 转换八进制转义序列

python - 难以按人口统计提取 IMDb 评级(使用 IMDbpy)

python - Django : how to Identify who is saving the model from program?

python - Keras 没有使用完整的 CPU 内核进行训练

python - 导入错误 : No module named yaml in Keras (neural network)

python - ValueError:检查目标时出错:预期 main_prediction 有 3 个维度,但得到形状为 (1128, 1) 的数组

python - imdbpy "nr_order"表中的 "cast_info"列代表什么?

mysql - 使用 IMDbPY 脚本导入 IMDb 文件时出现内存错误

__init__ 类中的 Python 函数指针