首页 文章

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

提问于
浏览
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)

但是当我尝试进行预测时,我会对跟踪追踪产生错误

Traceback(最近一次调用最后一次):文件“”,第1行,在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行,在runfile execfile(文件名,命名空间)文件“C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py“,第102行,在execfile exec中(编译(f.read(),filename,'exec'),命名空间)文件”C:/ Users /Radosław /nauka/python/SentimentAnalysis/sentiment_console.py“,第47行,在预测= model.predict(testArray)文件”C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ models.py“,第899行,在预测返回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 )文件“C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py”,第133行,在_standar dize_input_data str(array.shape))ValueError:检查时出错:预期embedding_1_input具有形状(None,100)但得到的形状为数组(1,3)

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

1 回答

  • 0

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

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

相关问题