• 将keras模型在django中应用时出现的小问题——ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 8), dtype=float32) is not an element of this graph.


    本文原出处(感谢作者提供):https://zhuanlan.zhihu.com/p/27101000

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

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

    王岳王院长王岳王院长
    10 个月前

    keras

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

    django

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

    问题背景

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

    问题描述

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

    # 加载模型,django 会在 web 应用初始化时执行这段代码
    from keras.models import load_model
    print '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 为经过了分词与向量化处理后的模型输入
    def category_class(comment):
        global model_COC
        result_vec = model_COC.predict(comment)
        return result_vec
    
    
    def emotion_class(comment):
        global model_COE
        result_vec = model_COE.predict(comment)
        return result_vec
    

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

    Traceback (most recent call last):
      File "/media/wave/D/workspace/LinuxPyCharm/actauto/classification/comments_class.py", line 38, in category_class
        result_vec = model_COC.predict(vecs)[0]
      File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 902, in predict
        return self.model.predict(x, batch_size=batch_size, verbose=verbose)
      File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1582, in predict
        self._make_predict_function()
      File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1049, in _make_predict_function
        **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2251, in function
        return Function(inputs, outputs, updates=updates)
      File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2205, in __init__
        with tf.control_dependencies(self.outputs):
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3595, in control_dependencies
        return get_default_graph().control_dependencies(control_inputs)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3324, in control_dependencies
        c = self.as_graph_element(c)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2414, in as_graph_element
        return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2493, in _as_graph_element_locked
        raise ValueError("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 应用初始化时执行这段代码
    from keras.models import load_model
    print '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...'
    print model_COC.predict(np.zeros((1, len(word_index)+1)))
    print model_COE.predict(np.zeros((1, len(word_index)+1)))
    print 'test done.'
    
    # 使用模型,在得到用户输入时会调用以下两个函数进行实时文本分类
    # 输入参数 comment 为经过了分词与向量化处理后的模型输入
    def category_class(comment):
        global model_COC
        result_vec = model_COC.predict(comment)
        return result_vec
    
    
    def emotion_class(comment):
        global model_COE
        result_vec = model_COE.predict(comment)
        return result_vec
    

    问题原因

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

  • 相关阅读:
    第十八章所有者权益部分考点知识点
    会计基础模拟练习一(3)
    会计基础模拟练习一(2)
    会计基础模拟练习一(1)
    第三章 复式记账
    第二章 会计科目和账户 笔记
    【Flink系列一】Flink开启Checkpoint,以及从Checkpoint恢复
    【已解决】解决Gradle在使用代理的情况下访问中央仓库返回403 Forbidden的问题
    Surface Laptop 3三个月使用心得
    Spring跨系统转发HTTP请求的简易实现(支持下载文件二进制流)
  • 原文地址:https://www.cnblogs.com/changbaishan/p/8631721.html
Copyright © 2020-2023  润新知