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()


This is quite the necro, but it's still a top search result for questions related to this and I thought it might help to make very explicit something that the prior answers (which are correct) note in passing:

The Q's variable product is a python variable. As such, it points to an object: When defined, it points to the tf.Tensor output of the matmul tf.Operation defined in the name_scope 'g1'. It is later redefined to point to a different object, the tf.Tensor output of 'g2'. This python variable has never heard of tf.name_scopes and doesn't care.

That is why you need to do lookups by the name attributes of the tensorflow objects... Those, by use of name_scopes, are unique and accessible. Or generate distinct python variables--that are unique and accessible according to python scoping rules--to point to each tf.Tensor object you want to reference.

Dunno if this is helpful for anybody else, but if I ever forget, I'll thank my past self for this.


I was having similar difficulties dealing with multiple graphs on a IPython notebook. What works for my purposes is to encapsulate each graph and its session in a function. I realize this is more of a hack I guess, I don't know anything about namespaces and I know OP wanted something along those lines. Maybe it will help someone I dunno, you can also pass results between computations.

import tensorflow as tf

def Graph1():
    g1 = tf.Graph()
    with g1.as_default() as g:
        matrix1 = tf.constant([[3., 3.]])
        matrix2 = tf.constant([[2.],[2.]])
        product = tf.matmul( matrix1, matrix2, name = "product")

    with tf.Session( graph = g ) as sess:
        tf.initialize_all_variables().run()
        return product


def Graph2(incoming):
    i = incoming
    g2 = tf.Graph()
    with g2.as_default() as g:
        matrix1 = tf.constant([[4., 4.]])
        matrix2 = tf.constant([[5.],[5.]])
        product = tf.matmul( matrix1, matrix2, name = "product" )

    with tf.Session( graph = g ) as sess:
        tf.initialize_all_variables().run()
        print product
        print i

print Graph1()

Graph2(Graph1())

Your problem is that you are calling the latest variable product which points to a tensor created on g2 - you overwrote it in your second scope. Just relabel all your variables and you should be good to go. The working code is below.

import tensorflow as tf

g1 = tf.Graph() with g1.as_default() as g:
    with g.name_scope( "g1" ) as scope:
        matrix11 = tf.constant([[3., 3.]])
        matrix21 = tf.constant([[2.],[2.]])
        product1 = tf.matmul(matrix11, matrix21)

tf.reset_default_graph()

g2 = tf.Graph() with g2.as_default() as g:
    with g.name_scope( "g2" ) as scope:
        matrix12 = tf.constant([[4., 4.]])
        matrix22 = tf.constant([[5.],[5.]])
        product2 = tf.matmul(matrix12, matrix22)

tf.reset_default_graph()

with tf.Session( graph = g1 ) as sess:
    result = sess.run( product1 )
    print( result )