title: Sharing variables between threads in Tensorflow
body: <p>I am trying to implement asynchronous gradient descent with TensorFlow using Python threads. In the main code, I define the graph, including a training operation, which gets a variable to keep count of the <code>global_step</code>:</p>
  
  <pre><code>with tf.variable_scope("scope_global_step") as scope_global_step:
      global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
  optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)
  train_op = optimizer.minimize(loss, global_step=global_step)
  </code></pre>
  
  <p>If I print the name of <code>global_step</code> I get:</p>
  
  <blockquote>
    <p>scope_global_step/global_step:0</p>
  </blockquote>
  
  <p>The main code also launches several threads to execute a <code>training</code> method:</p>
  
  <pre><code>threads = [threading.Thread(target=training, args=(sess, train_op, loss, scope_global_step)) for i in xrange(NUM_TRAINING_THREADS)]
  for t in threads: t.start()
  </code></pre>
  
  <p>I would like each thread to stop execution if the value of <code>global_step</code> is greater than or equal to <code>FLAGS.max_steps</code>. For that, I build the <code>training</code> method as it follows:</p>
  
  <pre><code>def training(sess, train_op, loss, scope_global_step):
      while (True):
           _, loss_value = sess.run([train_op, loss])
           with tf.variable_scope(scope_global_step, reuse=True): 
              global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
              global_step = global_step.eval(session=sess)
              if global_step &gt;= FLAGS.max_steps: break
  </code></pre>
  
  <p>This fails with the message:</p>
  
  <blockquote>
    <p>ValueError: Under-sharing: Variable scope_global_step/global_step does not exist, disallowed. Did you mean to set reuse=None in VarScope?</p>
  </blockquote>
  
  <p>I can see that <code>:0</code> is added to the name of the variable when it is first created, and when I try to retrieve it, that suffix is not used. Why is this? 
  If I manually add the suffix to the name of the variable when I try to retrieve it, it still claims the variable does not exist. Why does TensorFlow not find the variable? Shouldn't the variables automatically be shared among threads? I mean, all threads are running inside the same session, right? </p>
  
  <p>And another question related to my <code>training</code> method: will <code>global_step.eval(session=sess)</code> execute the graph again, or will it just fetch the value assigned to <code>gloabl_step</code> after the execution of the <code>train_op</code> and <code>loss</code> operations? In general, what is the recommended way of fetching values from variables to be used inside Python code?</p>