2017-04-19 36 views
2

使用IMDB的例子,我创建LSTM模式,尽量把自己的字符串预测情绪Keras错误时检查:预计embedding_1_input有形状(无,100),但得到阵列形状(1,3)

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号线,在 RUNFILE('C:/用户/拉多/ nauka /python/SentimentAnalysis/sentiment_console.py',wdir ='C:/ Users/R adosław/ nauka /蟒蛇/ SentimentAnalysis')

文件 “C:\ ProgramData \ Anaconda3 \ LIB \站点包\ Spyder的\ utils的\网站\ sitecustomize.py”,线路866,在RUNFILE 的execfile(文件名,命名空间)

执行文件中的文件“C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第102行 exec(compile(f.read(),filename,' EXEC“),命名空间)

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

文件 “C:\ ProgramData \ Anaconda3 \ lib中\站点包\ keras \ models.py”,线路899,在预测 返回self.model.predict(X,=的batch_size的batch_size,冗长=详细)

文件 “C:\ ProgramData \ Anaconda3 \ LIB \站点包\ keras \发动机\ training.py”,线路1555年,在预测 check_batch_axis = FALSE)

文件“C:\ ProgramData \ Anaconda3 \ LIB \ (array.shape))

ValueError:检查时出错:expected embedding_1_input to have形状(无,100),但有形状的阵列(1,3)

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

+0

如何定义您正在加载的模型?它可能定义了长度为100个字的输入形状。处理可变消息/文本长度的方法是填充短文本并剪切长文本,以便输入始终为100个字 –

回答

0

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

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