ValueError: Tensor must be from the same graph as Tensor with Bidirectinal RNN in Tensorflow

TensorFlow stores all operations on an operational graph. This graph defines what functions output to where, and it links it all together so that it can follow the steps you have set up in the graph to produce your final output. If you try to input a Tensor or operation on one graph into a Tensor or operation on another graph it will fail. Everything must be on the same execution graph.

Try removing with tf.Graph().as_default():

TensorFlow provides you a default graph which is referred to if you do not specify a graph. You are probably using the default graph in one spot and a different graph in your training block.

There does not seem to be a reason you are specifying a graph as default here and most likely you are using separate graphs on accident. If you really want to specify a graph then you probably want to pass it as a variable, not set it like this.


If you are using tf 2.x with Keras - then maybe disable-ling eager execution before building the model graph could help. So to disable eager execution - adding the following line before defining the model.

tf.compat.v1.disable_eager_execution()