ValueError: Tensor is not an element of this graph, when hosting a model in Sagemaker with Gunicorn and Flask and Keras
ValueError: Tensor is not an element of this graph, when hosting a model in Sagemaker with Gunicorn and Flask and Keras
我使用 Keras 创建了一个时间序列预测器,并根据 AWS 文档使用 Flash 和 Gunicorn 对模型进行了 Docker 化。我正在使用此代码加载序列化模型。
@classmethod
def get_model(cls):
if cls.model == None:
cls.model = load_model('/opt/ml/bitcoin_model.h5')
return cls.model
然后我使用预测方法生成结果,dockerized 容器在本地环境中运行良好,但是当我尝试在 sagemaker 中托管模型时它产生了这个错误。
ValueError: Tensor Tensor("dense_1/BiasAdd:0", shape=(?, 1), dtype=float32) is not an element of this graph.
那么我该如何解决这个问题?
问题已通过在模型加载阶段调用 _make_predict_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
我使用 Keras 创建了一个时间序列预测器,并根据 AWS 文档使用 Flash 和 Gunicorn 对模型进行了 Docker 化。我正在使用此代码加载序列化模型。
@classmethod
def get_model(cls):
if cls.model == None:
cls.model = load_model('/opt/ml/bitcoin_model.h5')
return cls.model
然后我使用预测方法生成结果,dockerized 容器在本地环境中运行良好,但是当我尝试在 sagemaker 中托管模型时它产生了这个错误。
ValueError: Tensor Tensor("dense_1/BiasAdd:0", shape=(?, 1), dtype=float32) is not an element of this graph.
那么我该如何解决这个问题?
问题已通过在模型加载阶段调用 _make_predict_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