title: Save and load Tensorflow model
body: <p>I want to save a Tensorflow (0.12.0) model, including graph and variable values, then later load and execute it. I have the read the docs and other posts on this but cannot get the basics to work. I am using the technique from <a href="https://www.tensorflow.org/versions/r0.10/how_tos/meta_graph/" rel="nofollow noreferrer">this page in the Tensorflow docs</a>. Code:</p>
  
  <p>Save a simple model:</p>
  
  <pre><code>myVar = tf.Variable(7.1)
  tf.add_to_collection('modelVariables', myVar) # why?
  init_op = tf.global_variables_initializer()
  with tf.Session() as sess:
      sess.run(init_op)
      print sess.run(myVar)
      saver0 = tf.train.Saver()
      saver0.save(sess, './myModel.ckpt')
      saver0.export_meta_graph('./myModel.meta')
  </code></pre>
  
  <p>Later, load and execute the model:</p>
  
  <pre><code>with tf.Session() as sess:
      saver1 = tf.train.import_meta_graph('./myModel.meta')
      saver1.restore(sess, './myModel.meta')
      print sess.run(myVar)
  </code></pre>
  
  <p><strong>Question 1:</strong>  The saving code seems to work but the loading code produces this error:</p>
  
  <pre><code>W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open ./myModel.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
  </code></pre>
  
  <p>How to fix this?.</p>
  
  <p><strong>Question 2:</strong> I included this line to follow the pattern in the TF docs...</p>
  
  <pre><code>tf.add_to_collection('modelVariables', myVar)
  </code></pre>
  
  <p>... but why is that line necessary? Doesn't <code>expert_meta_graph</code>export the entire graph by default? If not then does one need to add every variable in the graph to the collection before saving? Or do we just add to the collection those variables that will be accessed after the restore?</p>
  
  <p>---------------------- <em>Update January 12 2017</em> -----------------------------</p>
  
  <p>Partial success based on Kashyap's suggestion below but a mystery still exists. The code below works <em>but</em> only if I include the lines containing <code>tf.add_to_collection</code> and <code>tf.get_collection</code>. Without those lines, 'load' mode throws an error in the last line:
  <code>NameError: name 'myVar' is not defined</code>. My understanding was that by default <code>Saver.save</code> saves and restores all variables in the graph, so why is it necessary to specify the name of variables that will be used in the collection? I assume this has to do with mapping Tensorflow's variable names to Python names, but what are the rules of the game here? For which variables does this need to be done?</p>
  
  <pre><code>mode = 'load' # or 'save'
  if mode == 'save':
      myVar = tf.Variable(7.1)
      init_op = tf.global_variables_initializer()
      saver0 = tf.train.Saver()
      tf.add_to_collection('myVar', myVar) ### WHY NECESSARY?
      with tf.Session() as sess:
          sess.run(init_op)
          print sess.run(myVar)
          saver0.save(sess, './myModel')
  if mode == 'load':
      with tf.Session() as sess:
          saver1 = tf.train.import_meta_graph('./myModel.meta')
          saver1.restore(sess, tf.train.latest_checkpoint('./'))
          myVar = tf.get_collection('myVar')[0]  ### WHY NECESSARY?
          print sess.run(myVar)
  </code></pre>