title: How does reuse option in tf.variable_scope work?
body: <pre><code>from __future__ import print_function
  import tensorflow as tf
  
  def _var_init(name, shape, initializer=tf.contrib.layers.xavier_initializer(),
                trainable=True):
    with tf.device('/cpu:0'):
      var = tf.get_variable(
        name=name,
        shape=shape,
        initializer=initializer,
        trainable=trainable
      )
      return var
  
  def main():
    sess = tf.Session()
  
    # 1th case, it works
    with tf.variable_scope('test1', reuse=False) as test1:
      with tf.variable_scope('test2', reuse=False) as test2:
        w1 = _var_init('w1', [1, 2])
        sess.run(tf.global_variables_initializer())
        print(sess.run(w1), w1)
  
    # 2th case, it works
    with tf.variable_scope('test1', reuse=True):
      with tf.variable_scope('test2', reuse=False):
        w2 = _var_init('w1', [1, 2])
        print(sess.run(w2), w2)
  
    # 3th case, it works
    with tf.variable_scope(test1, reuse=False):
      with tf.variable_scope(test2, reuse=True):
        w3 = _var_init('w1', [1, 2])
        print(sess.run(w3), w3)
  
    # 4th case, ValueError: Variable test1/test2/w1 already exists.
    with tf.variable_scope(test1, reuse=True):
      with tf.variable_scope(test2, reuse=False):
        w4 = _var_init('w1', [1, 2])
        print(sess.run(w4), w4)
  
    # 5th case, ValueError: Variable test1/test2/w1 already exists.
    with tf.variable_scope('test1', reuse=False):
      with tf.variable_scope('test2', reuse=False):
        w5 = _var_init('w1', [1, 2])
        print(sess.run(w5), w5)
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>1th-3th case outputs:</p>
  
  <pre><code>[[ 0.34345531 -0.84748644]] &lt;tf.Variable 'test1/test2/w1:0' shape=(1, 2) dtype=float32_ref&gt;
  [[ 0.34345531 -0.84748644]] &lt;tf.Variable 'test1/test2/w1:0' shape=(1, 2) dtype=float32_ref&gt;
  [[ 0.34345531 -0.84748644]] &lt;tf.Variable 'test1/test2/w1:0' shape=(1, 2) dtype=float32_ref&gt;
  </code></pre>
  
  <p>Question: </p>
  
  <p>I'm confused why 2th case works but 4th fail. Doesn't Tensorflow search variable_scope by scope_name?  What's difference between 2th case and 4th case? (i.e. what's the difference between <code>with tf.variable_scope('test1', reuse=True):</code> and <code>with tf.variable_scope(test1, reuse=False):</code>?) </p>
  
  <p>I think they are same before. But now they looks different. How does reuse option in tf.variable_scope work?</p>
  
  <p>Similar but not duplicated Question：</p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/39234110/how-does-the-reuse-option-in-tf-variable-scope-work">How does the reuse option in tf.variable_scope work?
  </a>;</p></li>
  <li><p><a href="https://stackoverflow.com/questions/38488054/how-do-i-force-tf-variable-scope-to-reuse-name-scope">How do I force tf.variable_scope to reuse name_scope?
  </a></p></li>
  </ol>