title: NameError: free variable 'ar' referenced before assignment in enclosing scope
body: <p>I use tf.while_loop in simple code using tensorflow. It gives me this error </p>
  
  <blockquote>
    <p>ar = tf.cond(tf.less(arg[i], 1), lambda: tf.concat([ar,[1.0]], axis = 0), lambda: tf.concat([ar,[0.0]], axis = 0))</p>
    
    <p>NameError: free variable 'ar' referenced before assignment in enclosing scope</p>
  </blockquote>
  
  <p>the code::</p>
  
  <pre><code>a = tf.constant([[[1,2],[4,3]],[[5,6],[8,7]]])
  
  arg = tf.argmax(a, axis =2)
  
  arg = tf.cast(tf.reshape(arg,[4]), tf.int32)
  
  
  
  i =tf.constant(0)
  c = lambda i: tf.less(i,4)
  
  def body(i):
  
      ar = tf.cond(tf.less(arg[i], 1), lambda: tf.concat([ar,[1.0]], axis = 0), lambda: tf.concat([ar,[0.0]], axis = 0))
  
      print(i)
  
      i = tf.add(i,1)
  
      return i
  
  ar = tf.constant([1])
  
  r = tf.while_loop(c,body,[i])
  
  sess =tf.InteractiveSession()
  sess.run(tf.global_variables_initializer())
  
  sess.run(r)
  
  print(sess.run(ar))
  </code></pre>