Working with multiple graphs in TensorFlow

Your product is a global variable, and you’ve set it to point to “g2/MatMul”.

In particular

Try

print product

and you’ll see

Tensor("g2/MatMul:0", shape=(1, 1), dtype=float32)

So the system takes "g2/MatMul:0" since that’s the Tensor’s name, and tries to find it in the graph g1 since that’s the graph you set for the session. Incidentally you can see all nodes in the graph print [n.name for n in g1.as_graph_def().node]

Generally, using more than one graph is rarely useful. You can’t merge them and can’t pass tensors between them. I’d recommend just doing

tf.reset_default_graph()
a = tf.Constant(2)
sess = tf.InteractiveSession()
....

This way you’ll have one default graph and one default session and you can omit specifying graph or session in most cases. If you ever need to refer to them explicitly, you can get them from tf.get_default_graph() or tf.get_default_session()

Leave a Comment