Working with multiple graphs in TensorFlow
<p>Can someone explain to me how <code>name_scope</code> works in TensorFlow?  </p>

<p>Suppose I have the following code:</p>

<pre class="lang-py prettyprint-override"><code>import tensorflow as tf

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

tf.reset_default_graph()

g2 = tf.Graph()
with g2.as_default() as g:
    with g.name_scope( "g2" ) as scope:
        matrix1 = tf.constant([[4., 4.]])
        matrix2 = tf.constant([[5.],[5.]])
        product = tf.matmul(matrix1, matrix2)

tf.reset_default_graph()

with tf.Session( graph = g1 ) as sess:
    result = sess.run( product )
    print( result )
</code></pre>

<p>When I run this code I get the following error message:</p>

<pre><code>Tensor Tensor("g2/MatMul:0", shape=(1, 1), dtype=float32) is not an element of this graph.
</code></pre>

<p>I agree "g2/MatMul" is not an element of graph <code>g1</code>, but why is it selecting "g2/MatMul" when the session graph is set to <code>g1</code>? Why doesn't it select "g1/MatMul"?</p>

<hr>

<h3>Edit</h3>

<p>The following code seems to work:</p>

<pre class="lang-py prettyprint-override"><code>import tensorflow as tf

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

tf.reset_default_graph()

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

tf.reset_default_graph()

use_g1 = False

if ( use_g1 ):
    g = g1
    scope = g1_scope
else:
    g = g2
    scope = g2_scope

with tf.Session( graph = g ) as sess:
    tf.initialize_all_variables()
    result = sess.run( sess.graph.get_tensor_by_name( scope + "product:0" ) )
    print( result )
</code></pre>

<p>By flipping the switch <code>use_g1</code>, graph <code>g1</code> or <code>g2</code> will run in the session. Is this the way name scoping was meant to work?</p>