![](/img/trans.png)
[英]ValueError: Error when checking input: expected embedding_1_input to have shape (256,) but got array with shape (1,)
[英]Keras Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3)
我使用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')
运行文件execfile中的文件“ C:\\ ProgramData \\ Anaconda3 \\ lib \\ site-packages \\ spyder \\ utils \\ site \\ sitecustomize.py”,行866(文件名,名称空间)
文件“ C:\\ ProgramData \\ Anaconda3 \\ lib \\ site-packages \\ spyder \\ utils \\ site \\ sitecustomize.py”,第102行,位于execfile exec(编译(f.read(),文件名,'exec'),命名空间)
文件“ C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py”,第47行,位于预测= model.predict(testArray)中
预测返回self.model.predict(x,batch_size = batch_size,verbose = verbose)中的文件“ C:\\ ProgramData \\ Anaconda3 \\ lib \\ site-packages \\ keras \\ models.py”,行899
文件“ C:\\ ProgramData \\ Anaconda3 \\ lib \\ site-packages \\ keras \\ engine \\ training.py”,行1555,在预测中check_batch_axis = False)
_standardize_input_data str(array.shape)中的第133行的文件“ C:\\ ProgramData \\ Anaconda3 \\ lib \\ site-packages \\ keras \\ engine \\ training.py”
ValueError:检查时出错:预期embedding_1_input具有形状(无,100),但数组的形状为(1,3)
有没有适当的方法来重塑我的输入数组?
您已完成所有操作,但忘记了序列填充。 在调用预测之前添加此行。
testArray = sequence.pad_sequences(testArray, maxlen=maxlen)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.