ValueError: Tensor Tensor(…) is not an element of this graph. When using global variable keras model

二次信任 提交于 2019-12-05 18:45:59

Take a look at avital's answer on this github issue. Quoting the relevant part here:

Right after loading or constructing your model, save the TensorFlow graph:

graph = tf.get_default_graph()

In the other thread (or perhaps in an asynchronous event handler), do:

global graph
with graph.as_default():
    (... do inference here ...)

I modified this a bit and stored the graph in my app's config object instead of making it a global.

The TensorFlow documentation for get_default_graph explains why this is necessary:

NOTE: The default graph is a property of the current thread. If you create a new thread, and wish to use the default graph in that thread, you must explicitly add a with g.as_default(): in that thread's function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!