检查时Keras错误:预期embedding_1_input具有形状(无,100)但是具有形状的数组(1,3)

时间:2017-04-19 08:48:50

标签: 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)        

但是当我尝试做预测时,我得到了跟踪追踪错误

  

追踪(最近一次呼叫最后一次):

     

文件“”,第1行,in       RUNFILE( 'C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py',   WDIR = 'C:/用户/拉多/ nauka /蟒/ SentimentAnalysis')

     

文件   “C:\ ProgramData \ Anaconda3 \ LIB \站点包\ Spyder的\ utils的\网站\ sitecustomize.py”   第866行,在runfile中       execfile(filename,namespace)

     

文件   “C:\ ProgramData \ Anaconda3 \ LIB \站点包\ Spyder的\ utils的\网站\ sitecustomize.py”   第102行,在execfile中       exec(compile(f.read(),filename,'e​​xec'),namespace)

     

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

     

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

     

文件   “C:\ ProgramData \ Anaconda3 \ LIB \站点包\ keras \发动机\ training.py”   第1555行,预测       check_batch_axis =假)

     

文件   “C:\ ProgramData \ Anaconda3 \ LIB \站点包\ keras \发动机\ training.py”   第133行,在_standardize_input_data中       STR(array.shape))

     

ValueError:检查时出错:预期embedding_1_input有   形状(无,100),但有阵列形状(1,3)

是否有正确的方法来重塑我的输入数组?

1 个答案:

答案 0 :(得分:0)

你做了一切,但忘记了序列填充。在调用预测之前添加此行。

testArray = sequence.pad_sequences(testArray, maxlen=maxlen)
相关问题