title: Error upon restoring variable
body: <p>I stumbled across an error that I am unable to resolve. What I am trying to do is the following thing: </p>
  
  <p>I want to train a (dummy) model that adds <code>a</code> to <code>b</code> on every iteration. When finished, I want to save the variables as checkpoint. The first time I run it, it shall build the model from scratch. Every time I re-run the model, it should start from the last checkpoint and do the additions again. Hereby, I load the complete graph from the .meta file. The global step variable is there to keep track of the total number of steps I have trained.</p>
  
  <pre><code>import tensorflow as tf
  from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
  
  # List ALL tensors.
  print_tensors_in_checkpoint_file(tf.train.latest_checkpoint('./'), all_tensors=True, tensor_name='')
  
  tf.reset_default_graph()
  
  global_step = tf.get_variable('global_step', shape=[], dtype=tf.int32, initializer=tf.constant_initializer(0), trainable=False)
  
  def model(a, b):
      b = tf.assign_add(b, a)
      return b
  
  with tf.Session() as sess:
  
      ckpt = tf.train.latest_checkpoint('./')
      if ckpt:
          saver = tf.train.import_meta_graph('./my_test_model-1.meta')
          saver.restore(sess, ckpt)
  
      else:
          a = tf.Variable(3.0, name='a')
          b = tf.Variable(5.0, name='b')
  
          b = model(a, b)
  
          ### before EDIT
          saver = tf.train.Saver()
          sess.run(tf.global_variables_initializer())
          ###
  
          ### after EDIT
          sess.run(tf.global_variables_initializer())
          saver = tf.train.Saver()
          ###
  
      for step in range(5):
          global_step.assign_add(1).eval()
          print(global_step.eval())
          print(b.eval())
  
          saver.save(sess, './my_test_model', global_step=global_step)
  </code></pre>
  
  <p>The script runs fine for the first time, outputting this:</p>
  
  <pre><code>1        # step
  8.0      # value of b
  2
  11.0
  3
  14.0
  4
  17.0
  5
  20.0
  </code></pre>
  
  <p>The second time I run the program, I get this output followed by an error:</p>
  
  <pre><code>tensor_name:  a
  3.0
  tensor_name:  b
  20.0
  tensor_name:  global_step
  0
  tensor_name:  global_step_1
  5
  
  INFO:tensorflow:Restoring parameters from ./my_test_model-5
  </code></pre>
  
  <blockquote>
    <p>Traceback (most recent call last): ... FailedPreconditionError:
    Attempting to use uninitialized value global_step      [[Node:
    AssignAdd_2 = AssignAdd[T=DT_INT32, use_locking=false,
    _device="/job:localhost/replica:0/task:0/device:CPU:0"](global_step, AssignAdd_2/value)]] ...</p>
  </blockquote>
  
  <p>The first time, it's clear that it won't throw an error as I run the initializer for all variables. But I thought that restoring a model counts as some sort of initialization? I really cannot wrap my head around this concept. I also tried defining <code>global_step</code> after defining <code>a</code> and <code>b</code>, but this resulted in another error when loading for the first time:</p>
  
  <blockquote>
    <p>ValueError: Cannot use the default session to evaluate tensor: the
    tensor's graph is different from the session's graph. Pass an explicit
    session to <code>eval(session=sess)</code>.
    The error refers to the the line that increments <code>global_step</code> (<code>global_step.assign_add(1).eval()</code>). </p>
  </blockquote>
  
  <p>What am I doing wrong? Where should I define the variable?</p>
  
  <p>I appreciate any help on this problem! Thank you for reading this far.</p>
  
  <p><strong>EDIT:</strong>
  Thanks to @Diana, the precondition error vanished. Unfortunately, another error occured. Whenever running the script with loading a checkpoint, it throws a name error:</p>
  
  <blockquote>
    <p>NameError: name 'global_step' is not defined.</p>
  </blockquote>
  
  <p>This also happens for variable ´b´. Shouldn't be the name loaded when restoring the checkpoint? The tensors seem to have the right names and values when I check the tensors in the checkpoint file.</p>