‘Tensor (“something”) is not an element of this graph.’ Error in Keras using Tensorflow backend on Flask Web Server.

Kobkrit Viriyayudhakorn
Kobkrit
Published in
2 min readOct 26, 2018

--

I am deploying my Keras model (using Tensorflow backend) on the web service using Flask. When I test inference the model by firing the result from Postman to test the result, the message like this shown up:

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

After research a while, every web request handled by Flask, it will create a new threads (or something similar to threads) which will generate their own Tensorflow session, Not the default one that we have loaded with our models.

To fix this, We just tell them to use the default session that loaded with our models. By adding the following statements.

graph = tf.get_default_graph()

Right after when your model is loaded.

And then when you want to inference, add

global graph
with graph.as_default():

Then you can,

Run it successfully.

Hope you enjoy!

For those whoever want to develop and get consult on creating your own AI model, please getting more information at our company website “iApp Technology” (https://iapp.co.th) and testing our AI demoes (https://ai.iapp.co.th). You can contact me directly at kobkrit@iapp.co.th. Thank you very much. #iApp #Ai

See more at https://iapp.co.th and https://ai.iapp.co.th

--

--