解决在django中应用keras模型时出现的ValueError("Tensor %s is not an element of this graph." % obj)问题

用keras训练好模型,再在django初始化加载模型,这个过程没有问题,但是在调用到模型执行model.predict()的时候就报错:

 raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.

解决方法:

在django中加载模型后,先执行一次model.predict()操作,之后的调用就不会出问题了

model = load_model(filepath=model_path)
y = model.predict(x)

 

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

posted @ 2018-04-26 21:03  焦距  阅读(1684)  评论(0编辑  收藏  举报