TensorFlow: how to name operations for tf.get_variable
<p>My question is related to this <a href="http://stackoverflow.com/questions/36612512/tensorflow-how-to-get-a-tensor-by-name">Tensorflow: How to get a tensor by name?</a></p>

<p>I can give names to operations. But actually they named differently.
For example:</p>

<pre><code>In [11]: with tf.variable_scope('test_scope') as scope:
    ...:     a = tf.get_variable('a',[1])
    ...:     b = tf.maximum(1,2, name='b')
    ...:     print a.name
    ...:     print b.name
    ...:     
    ...:     
    ...:     
test_scope/a:0
test_scope_1/b:0

In [12]: with tf.variable_scope('test_scope') as scope:
    ...:     scope.reuse_variables()
    ...:     a = tf.get_variable('a',[1])
    ...:     b = tf.maximum(1,2, name='b')
    ...:     print a.name
    ...:     print b.name
    ...:     
    ...:     
    ...:     
test_scope/a:0
test_scope_2/b:0
</code></pre>

<p><code>tf.get_variable</code> creates variable with exactly the same name as I ask. Operations add prefixes to scope. </p>

<p>I want to name my operation so that I can get it. In my case I want to get <code>b</code> with <code>tf.get_variable('b')</code> in my scope.</p>

<p>How can I do it? I can't do it with <code>tf.Variable</code> because of this issue <a href="https://github.com/tensorflow/tensorflow/issues/1325" rel="nofollow">https://github.com/tensorflow/tensorflow/issues/1325</a>
May be I need to set addition parameters to variable scope, or to operation, or somehow use <code>tf.get_variable</code> ?</p>

<p>I disagree with @rvinas answer, you don't need to create a Variable to hold the value of a tensor you want to retrieve. You can just use <code>graph.get_tensor_by_name</code> with the correct name to retrieve your tensor:</p>

<pre class="lang-py prettyprint-override"><code>with tf.variable_scope('test_scope') as scope:
    a = tf.get_variable('a',[1])
    b = tf.maximum(1,2, name='b')

print a.name  # should print 'test_scope/a:0'
print b.name  # should print 'test_scope/b:0'
</code></pre>

<hr>

<p>Now you want to recreate the same scope and get back <code>a</code> and <code>b</code>.<br>
For <code>b</code>, you don't even need to be in the scope, you just need the exact name of <code>b</code>.</p>

<pre class="lang-py prettyprint-override"><code>with tf.variable_scope('test_scope') as scope:
    scope.reuse_variables()
    a2 = tf.get_variable('a', [1])

graph = tf.get_default_graph()
b2 = graph.get_tensor_by_name('test_scope/b:0')

assert a == a2
assert b == b2
</code></pre>

python tensorflow