代码之家  ›  专栏  ›  技术社区  ›  Nirojan Selvanathan

ValueError:当使用Gunicorn、Flask和Keras在Sagemaker中托管模型时,张量不是此图的元素

  •  1
  • Nirojan Selvanathan  · 技术社区  · 7 年前

    @classmethod
    def get_model(cls):
        if cls.model == None:
            cls.model = load_model('/opt/ml/bitcoin_model.h5')
        return cls.model
    

    然后我使用预测方法产生结果,停靠的容器在本地环境中工作得很好,但是当我尝试在sagemaker中托管模型时,它产生了这个错误。

    ValueError: Tensor Tensor("dense_1/BiasAdd:0", shape=(?, 1), dtype=float32) is not an element of this graph.
    

    那么我该如何解决这个问题呢?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Nirojan Selvanathan    7 年前

    通过在模型加载阶段调用\u make \u predict \u function()方法解决了这个问题。

    @classmethod
    def get_model(cls):
        if cls.model == None:
            cls.model = load_model('/opt/ml/bitcoin_model.h5')
            cls.model._make_predict_function()
        return cls.model
    

    错误参考: https://github.com/keras-team/keras/issues/6462