美文网首页
keras+flask进行预测时遇到的玄学问题

keras+flask进行预测时遇到的玄学问题

作者: 美环花子若野 | 来源:发表于2018-10-31 11:28 被阅读163次

    https://zhuanlan.zhihu.com/p/27101000

    将keras模型在django中应用时出现的小问题

    王岳王院长

    keras

    一个做深度学习的框架,可以训练深度学习的模型,这里后端使用的是 tensorflow

    django

    一个 python 语言的 web 框架,可以做 web 应用

    问题背景

    项目需求是用深度学习训练一个文本分类的模型,然后在 web 应用中加载这个训练好的模型在利用模型对实时输入的文本进行分类,这样用户在浏览器页面中输入文本,就可以立刻得到模型返回的文本分类结果

    问题描述

    模型有两个,一个情感分类,一个内容类别分类,用 keras 训练好了模型,用 h5 格式进行保存。再在 django 中加载这个模型,模型文件分别为 cate_class_model.h5 与 emo_class_model.h5 ,加载与使用相关的代码如下

    # 加载模型,django 会在 web 应用初始化时执行这段代码fromkeras.modelsimportload_modelprint'load model...'model_COC=load_model(sys.path[0]+'/resource/cate_class_model.h5')model_COE=load_model(sys.path[0]+'/resource/emo_class_model.h5')print'load done.'# 使用模型,在得到用户输入时会调用以下两个函数进行实时文本分类# 输入参数 comment 为经过了分词与向量化处理后的模型输入defcategory_class(comment):globalmodel_COCresult_vec=model_COC.predict(comment)returnresult_vecdefemotion_class(comment):globalmodel_COEresult_vec=model_COE.predict(comment)returnresult_vec

    django 初始化加载模型的过程没有问题,但是一旦调用函数使用模型时执行到 model.predict 就会报错

    Traceback (most recent call last):File"/media/wave/D/workspace/LinuxPyCharm/actauto/classification/comments_class.py", line38, incategory_classresult_vec=model_COC.predict(vecs)[0]File"/usr/local/lib/python2.7/dist-packages/keras/models.py", line902, inpredictreturnself.model.predict(x,batch_size=batch_size,verbose=verbose)File"/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line1582, inpredictself._make_predict_function()File"/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line1049, in_make_predict_function**kwargs)File"/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line2251, infunctionreturnFunction(inputs,outputs,updates=updates)File"/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line2205, in__init__withtf.control_dependencies(self.outputs):File"/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line3595, incontrol_dependenciesreturnget_default_graph().control_dependencies(control_inputs)File"/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line3324, incontrol_dependenciesc=self.as_graph_element(c)File"/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line2414, inas_graph_elementreturnself._as_graph_element_locked(obj,allow_tensor,allow_operation)File"/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line2493, in_as_graph_element_lockedraiseValueError("Tensor %s is not an element of this graph."%obj)ValueError:Tensor Tensor("dense_2/Softmax:0", shape=(?, 8), dtype=float32) is not an element of this graph.

    在正常的 python 程序里运行没有问题,在 django 里将模型加载与模型使用的代码放在一起也没有问题,但是一旦将加载与使用的过程分开就会出现这个问题

    解决方法

    在初始化加载模型之后,就随便生成一个向量让 model 执行一次 predict 函数,之后再使用就不会有问题了

    # 加载模型,django 会在 web 应用初始化时执行这段代码fromkeras.modelsimportload_modelprint'load model...'model_COC=load_model(sys.path[0]+'/resource/cate_class_model.h5')model_COE=load_model(sys.path[0]+'/resource/emo_class_model.h5')print'load done.'# load 进来模型紧接着就执行一次 predict 函数print'test model...'printmodel_COC.predict(np.zeros((1,len(word_index)+1)))printmodel_COE.predict(np.zeros((1,len(word_index)+1)))print'test done.'# 使用模型,在得到用户输入时会调用以下两个函数进行实时文本分类# 输入参数 comment 为经过了分词与向量化处理后的模型输入defcategory_class(comment):globalmodel_COCresult_vec=model_COC.predict(comment)returnresult_vecdefemotion_class(comment):globalmodel_COEresult_vec=model_COE.predict(comment)returnresult_vec

    问题原因

    才疏学浅啊,原因不明,解决方法也甚是神奇,不愧机器玄学,醉了

    相关文章

      网友评论

          本文标题:keras+flask进行预测时遇到的玄学问题

          本文链接:https://www.haomeiwen.com/subject/amaotqtx.html