title: Tensorflow tf.cond and multiline lambdas
body: <p>I'm playing around with <code>tf.cond</code> and <code>tf.while_loop</code>. I have the following code:</p>
  
  <pre><code>def cond_loop_test(t1, num_steps):
      def cond(i, t1, t2, t3, t4):
          return tf.less(i, num_steps)
  
      def body(i, t1, t2, t3, t4):
          t3, t4 = t3+1, t4+1
  
          def last_step(t1, t2):
              t1 = t1+2
              t2 = t2+2
              def f1(): return t1, t2
              return f1
  
          def other_steps(t1, t2):
              t1 = t1+1
              t2 = t2+1
              def f2(): return t1, t2
              return f2
  
          tf.cond(tf.equal(i+1, num_steps), last_step(t1, t2), other_steps(t1, t2))
  
          return [tf.add(i, 1), t1, t2, t3, t4]
  
      return tf.while_loop(cond, body, [0,
                                        t1,
                                        tf.zeros_like(t1),
                                        tf.zeros_like(5),
                                        tf.zeros_like(5)])
  
  t1 = tf.ones(5)
  num_steps = tf.constant(5)
  
  with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      cond_loop = cond_loop_test(t1, num_steps)
      print(sess.run(cond_loop))
  </code></pre>
  
  <p>The code basically runs 5 loops with a simple conditional which should run different code at different stage during the loop execution of the graph.</p>
  
  <p>When I run it, I get the following output:</p>
  
  <pre><code>[5, array([1., 1., 1., 1., 1.], dtype=float32), array([0., 0., 0., 0., 0.], dtype=float32), 5, 5]
  </code></pre>
  
  <p>As you can see tensors <code>t1</code> and <code>t2</code> do not get modified despite their being modified inside functions <code>last_step</code> and <code>other_steps</code> - these functions return no-argument function which are required by <code>tf.cond</code>.</p>
  
  <p>What I would expect is something like this:</p>
  
  <pre><code>[5, array([8., 8.,8., 8., 8.], dtype=float32), array([7., 7., 7., 7., 7.], dtype=float32), 5, 5]
  </code></pre>
  
  <p>Basically both <code>t1</code> and <code>t2</code> would be incremented 4 times and then added 2 in the last iteration loop.</p>
  
  <p><strong>Note:</strong> I can't use multiline lambda here because if I did python would cry about <code>UnboundLocalError: local variable  referenced before assignment</code>, hence the ugly python hack which merely returns zero-arg function which returns the modified tensors (kinda getting used to hacks in this language at this point).</p>
  
  <p>Can someone help me out understanding why aren't tensors <code>t1</code> and <code>t2</code> modified in the above code? As you can see tensors <code>t3</code> and <code>t4</code> do get modified correctly, alas they're not modified inside the conditional functions.</p>