title: Building Tensorflow Graphs Inside of Functions
body: <p>I'm learning Tensorflow and am trying to properly structure my code. I (more or less) know how to build graphs either bare or as class methods, but I'm trying to figure out how best to structure the code. I've tried the simple example:</p>
  
  <pre><code>def build_graph():                
       g = tf.Graph()     
       with g.as_default():                       
           a = tf.placeholder(tf.int8)
           b = tf.add(a, tf.constant(1, dtype=tf.int8))
       return g   
  
  graph = build_graph()
  with tf.Session(graph=graph) as sess:
       feed = {a: 3}      
       print(sess.run(b, feed_dict=feed))
  </code></pre>
  
  <p>which should just print out 4. However, when I do that, I get the error: </p>
  
  <pre><code>Cannot interpret feed_dict key as Tensor: Tensor 
  Tensor("Placeholder:0", dtype=int8) is not an element of this graph.
  </code></pre>
  
  <p>I'm pretty sure this is because the placeholder inside the function <code>build_graph</code> is private, but shouldn't the <code>with tf.Session(graph=graph)</code> take care of that? Is there a better way of using a feed dict in a situation like this?</p>