from keras.datasets import imdb
from keras.models import load_model
deep = load_model('ImdbLSTMtry.h5')
from keras import preprocessing
import numpy as np
(xtrain,ytrain) , (xtest,ytest) = imdb.load_data(num_words=10000)
xtrain = preprocessing.sequence.pad_sequences(xtrain,maxlen=500)
xtest = preprocessing.sequence.pad_sequences(xtest,maxlen=500)
print(xtest[0].shape)
result = deep.predict(xtest[0])
from keras.datasets import imdb
from keras import preprocessing
max_fea = 10000
mal = 500
(xtrain,ytrain) , (xtest,ytest) = imdb.load_data(num_words=max_fea)
xtrain = preprocessing.sequence.pad_sequences(xtrain,maxlen=mal)
xtest = preprocessing.sequence.pad_sequences(xtest,maxlen=mal)
from keras.models import Sequential
from keras.layers import Embedding , Flatten , Dense , SimpleRNN, LSTM, GRU
model = Sequential()
model.add(Embedding(10000,8,input_length=mal))
model.add(GRU(32))
model.add(Dense(1,activation='sigmoid')) model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics['accuracy'])
model.fit(xtrain,ytrain,epochs=1,batch_size=128,validation_split=0.2)
print(xtest[0])
result = model.evaluate(xtest,ytest)
print(result)
model.save('ImdbLSTMtry.h5')
model.summary()
我试图从喀拉斯邦的Imdb数据集中预测情感分析,我改变了输入的形状,就像嵌入层的形状一样,但是有错误。
答案 0 :(得分:1)
您应该尝试:
result = deep.predict(xtest)
方法predict
不仅可以预测一个样本(在您的情况下为xtest[0]
),还可以预测整个数据集(xtest
)。
如果您只想对xtest[0]
进行预测,则可以按以下步骤进行操作:
result = deep.predict(np.expand_dims(xtest[0], axis=0))