Keras检查时出错:预期embedding_1_input具有形状(无,100),但数组的形状为(1,3) [英] Keras Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3)

查看:265
本文介绍了Keras检查时出错:预期embedding_1_input具有形状(无,100),但数组的形状为(1,3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用imdb示例创建了LSTM模型,并尝试预测自己的字符串中的情绪

I created LSTM model using imdb example and tried to predict sentiment in my own string

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)        

但是当我尝试进行预测时,我的跟踪回溯出现了错误

But when I try to do prediction I get errored with following traceback

回溯(最近通话最近一次):

Traceback (most recent call last):

文件",第1行,在 runfile('C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py', wdir ='C:/Users/Radosław/nauka/python/SentimentAnalysis')

File "", line 1, in runfile('C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py', wdir='C:/Users/Radosław/nauka/python/SentimentAnalysis')

文件 "C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py", 运行文件中的第866行 execfile(文件名,命名空间)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile execfile(filename, namespace)

文件 "C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py", 第102行,在execfile中 exec(compile(f.read(),文件名,'exec'),命名空间)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

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

File "C:/Users/Radosław/nauka/python/SentimentAnalysis/sentiment_console.py", line 47, in prediction = model.predict(testArray)

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

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)

文件 "C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py", 1555行,在预测中 check_batch_axis = False)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1555, in predict check_batch_axis=False)

文件 "C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py", _standardize_input_data中的第133行 str(array.shape))

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 133, in _standardize_input_data str(array.shape))

ValueError:检查时出错:预期embedding_1_input具有 形状(无,100),但得到的数组形状为(1,3)

ValueError: Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3)

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

is there is proper way to reshape my input array?

推荐答案

您做了所有事情,但是忘记了序列填充.在调用预报之前添加此行.

You did everything but forgot the sequence padding. Add this line before calling predict.

testArray = sequence.pad_sequences(testArray, maxlen=maxlen)

这篇关于Keras检查时出错:预期embedding_1_input具有形状(无,100),但数组的形状为(1,3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆