title: How to optimize for inference a simple, saved TensorFlow 1.0.1 graph?
body: <p>I cannot successfully run the <code>optimize_for_inference</code> module on a simple, saved TensorFlow graph (Python 2.7; package installed by <code>pip install tensorflow-gpu==1.0.1</code>).</p>
  
  <h1>Background</h1>
  
  <h2>Saving TensorFlow Graph</h2>
  
  <p>Here's my Python script to generate and save a simple graph to add 5 to my input <code>x</code> <code>placeholder</code> operation.</p>
  
  <pre><code>import tensorflow as tf
  
  # make and save a simple graph
  G = tf.Graph()
  with G.as_default():
      x = tf.placeholder(dtype=tf.float32, shape=(), name="x")
      a = tf.Variable(5.0, name="a")
      y = tf.add(a, x, name="y")
      saver = tf.train.Saver()
  
  with tf.Session(graph=G) as sess:
      sess.run(tf.global_variables_initializer())
      out = sess.run(fetches=[y], feed_dict={x: 1.0})
      print(out)
      saver.save(sess=sess, save_path="test_model")
  </code></pre>
  
  <h2>Restoring TensorFlow Graph</h2>
  
  <p>I have a simple restore script that recreates the saved graph and restores graph params. Both the save/restore scripts produce the same output.</p>
  
  <pre><code>import tensorflow as tf
  
  # Restore simple graph and test model output
  G = tf.Graph()
  
  with tf.Session(graph=G) as sess:
      # recreate saved graph (structure)
      saver = tf.train.import_meta_graph('./test_model.meta')
      # restore net params
      saver.restore(sess, tf.train.latest_checkpoint('./'))
  
      x = G.get_operation_by_name("x").outputs[0]
      y = G.get_operation_by_name("y").outputs
      out = sess.run(fetches=[y], feed_dict={x: 1.0})
      print(out[0])
  </code></pre>
  
  <h2>Optimization Attempt</h2>
  
  <p>But, while I don't expect much in terms of optimization, when I try to optimize the graph for inference, I get the following error message. The expected output node does not appear to be in the saved graph.</p>
  
  <pre><code>$ python -m tensorflow.python.tools.optimize_for_inference --input test_model.data-00000-of-00001 --output opt_model --input_names=x --output_names=y  
  Traceback (most recent call last):  
    File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main  
      "__main__", fname, loader, pkg_name)  
    File "/usr/lib/python2.7/runpy.py", line 72, in _run_code  
      exec code in run_globals  
    File "/{path}/lib/python2.7/site-packages/tensorflow/python/tools/optimize_for_inference.py", line 141, in &lt;module&gt;  
      app.run(main=main, argv=[sys.argv[0]] + unparsed)  
    File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run  
      _sys.exit(main(_sys.argv[:1] + flags_passthrough))
    File "/{path}/lib/python2.7/site-packages/tensorflow/python/tools/optimize_for_inference.py", line 90, in main  
      FLAGS.output_names.split(","), FLAGS.placeholder_type_enum)  
    File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/tools/optimize_for_inference_lib.py", line 91, in optimize_for_inference  
      placeholder_type_enum)  
    File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/tools/strip_unused_lib.py", line 71, in strip_unused  
      output_node_names)  
    File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/framework/graph_util_impl.py", line 141, in extract_sub_graph  
      assert d in name_to_node_map, "%s is not in graph" % d  
  AssertionError: y is not in graph  
  </code></pre>
  
  <p>Further investigation led me to inspect the checkpoint of the saved graph, which only shows 1 tensor (<code>a</code>, no <code>x</code> and no <code>y</code>).</p>
  
  <pre><code>(tf-1.0.1) $ python -m tensorflow.python.tools.inspect_checkpoint --file_name ./test_model --all_tensors
  tensor_name:  a
  5.0
  </code></pre>
  
  <h1>Specific Questions</h1>
  
  <ol>
  <li>Why do I not see <code>x</code> and <code>y</code> in the checkpoint? Is it because they are operations and not tensors?</li>
  <li>Since I need to provide input and output names to the <code>optimize_for_inference</code> module, how do I build the graph so I can reference the input and output nodes?</li>
  </ol>