Keras学习(十)-加载已经训练好的模型进行预测(以全连接imdb为例)
2019-04-19 14:36
471 查看
#加载已经训练好的模型进行预测的 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预训练好的模型进行目标类别预测
- 深度学习-CAFFE利用CIFAR10网络模型训练自己的图像数据获得模型-4应用生成模型进行预测
- 如何将Keras模型保存到文件中,并再次加载它们来进行预测。
- TensorFlow学习实践(四):使用TFRecord格式数据和tf.contrib.slim API进行模型训练和预测
- 深度学习笔记--使用keras训练&保存&加载CNN模型
- TensorFlow学习实践(三):使用TFRecord格式数据和tf.estimator API进行模型训练和预测
- Keras中如何保存和加载训练好的预测模型——长短期记忆(LSTM)系列_LSTM的建模方法(4)
- TensorFlow学习实践(一):使用TFRecord格式数据和队列进行模型训练和预测
- 【神经网络与深度学习】Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
- 深度学习Caffe平台实例——CIFAR-10数据集在caffe平台上模型训练及实例使用模型进行预测
- TensorFlow学习实践(二):使用TFRecord格式数据和tf.data API进行模型训练和预测
- 使用三种继承回归模型对美国波士顿房价训练数据进行学习,并对测试数据进行预测
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载
- tensorflow学习笔记六:保存和加载训练模型
- tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测
- Tensorflow保存模型,恢复模型,使用训练好的模型进行预测和提取中间输出(特征)
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载
- 集成模型(回归)对Boston数据进行训练并对数据做出预测
- keras系列︱迁移学习:利用InceptionV3进行fine-tuning及预测、完美案例(五)
- 转:tensorflow深度学习实战笔记(二):把训练好的模型进行固化