Keras Error when checking : expected embedding_1_i

2019-05-10 02:43发布

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

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)

is there is proper way to reshape my input array?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-10 03:17

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

testArray = sequence.pad_sequences(testArray, maxlen=maxlen)
查看更多
登录 后发表回答