Keras学习(十)-加载已经训练好的模型进行预测(以全连接imdb为例)

#加载已经训练好的模型进行预测的
from keras.models import load_model
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
from keras.models import model_from_json
from keras.models import model_from_yaml

#加载方法一
# 加载模型结构
# model = model_from_yaml(open('../docs/keras/model_architecture.yaml').read())
model = model_from_json(open('SaveModel/Imdb_MLP_model.json').read())

#加载方式二
with open(r'SaveModel/Imdb_MLP_model.json', 'r') as file:
    model_json1 = file.read()
model = model_from_json(model_json1)  

# 加载模型参数
model.load_weights('SaveModel/Imdb_MLP_model.h5')

#输入新的影评
input_text='''
Oh dear, oh dear, oh dear: where should I start folks. I had low expectations already because I hated each and every single trailer so far, but boy did Disney make a blunder here. I'm sure the film will still make a billion dollars - hey: if Transformers 11 can do it, why not Belle? - but this film kills every subtle beautiful little thing that had made the original special, and it does so already in the very early stages. It's like the dinosaur stampede scene in Jackson's King Kong: only with even worse CGI (and, well, kitchen devices instead of dinos).
The worst sin, though, is that everything (and I mean really EVERYTHING) looks fake. What's the point of making a live-action version of a beloved cartoon if you make every prop look like a prop? I know it's a fairy tale for kids, but even Belle's village looks like it had only recently been put there by a subpar production designer trying to copy the images from the cartoon. There is not a hint of authenticity here. Unlike in Jungle Book, where we got great looking CGI, this really is the by-the-numbers version and corporate filmmaking at its worst. Of course it's not really a "bad" film; those 200 million blockbusters rarely are (this isn't 'The Room' after all), but it's so infuriatingly generic and dull - and it didn't have to be. In the hands of a great director the potential for this film would have been huge.
Oh and one more thing: bad CGI wolves (who actually look even worse than the ones in Twilight) is one thing, and the kids probably won't care. But making one of the two lead characters - Beast - look equally bad is simply unforgivably stupid. No wonder Emma Watson seems to phone it in: she apparently had to act against an guy with a green-screen in the place where his face should have been. 
'''

token = Tokenizer(num_words=2000)
# token.fit_on_texts(train_text)
#将影评转化为数字列表
input_seq = token.texts_to_sequences([input_text])#将影评转化为数字列表

#进行取长补短操作
pad_input_seq  = sequence.pad_sequences(input_seq , maxlen=100)#进行取长补短操作

predict_result=model.predict_classes(pad_input_seq)#放入模型进行分析
predict_result[0][0]
“”“ 0 ”“”

#建立中文标示情感函数
SentimentDict={1:'正面的',0:'负面的'}
def display_test_Sentiment(i):
    print(test_text[i])
    print('标签label:',SentimentDict[y_test[i]],
          '预测结果:',SentimentDict[predict_classes[i]])

SentimentDict[predict_result[0][0]]
“”“ '负面的' ”“”

你可能感兴趣的:(Keras)