title: Side effect in tf.while_loop
body: <p>I am currently having a hard time trying to understand how tensorflow works, and I feel like the python interface is somehow obscure.</p>
  
  <p>I recently tried to run a simple print statement inside a tf.while_loop, and there are many things that remains unclear to me:</p>
  
  <pre><code>import tensorflow as tf
  
  nb_iter = tf.constant(value=10)
  #This solution does not work at all
  #nb_iter = tf.get_variable('nb_iter', shape=(1), dtype=tf.int32, trainable=False)
  i = tf.get_variable('i', shape=(), trainable=False,
                       initializer=tf.zeros_initializer(), dtype=nb_iter.dtype)
  
  loop_condition = lambda i: tf.less(i, nb_iter)
  def loop_body(i):
      tf.Print(i, [i], message='Another iteration')
      return [tf.add(i, 1)]
  
  i = tf.while_loop(loop_condition, loop_body, [i])
  
  initializer_op = tf.global_variables_initializer()
  
  with tf.Session() as sess:
      sess.run(initializer_op)
      res = sess.run(i)
      print('res is now {}'.format(res))
  </code></pre>
  
  <p>Notice that if I initialize nb_iter with</p>
  
  <pre><code>nb_iter = tf.get_variable('nb_iter', shape=(1), dtype=tf.int32, trainable=False)
  </code></pre>
  
  <p>I got the following error:</p>
  
  <blockquote>
    <p>ValueError: Shape must be rank 0 but is rank 1 for 'while/LoopCond'
    (op: 'LoopCond') with input shapes: [1].</p>
  </blockquote>
  
  <p>It get even worse when I try to use the 'i' index for indexing a tensor (example not shown here), I then get the following error</p>
  
  <blockquote>
    <p>alueError: Operation 'while/strided_slice' has been marked as not
    fetchable.</p>
  </blockquote>
  
  <p>Can someone point me to a documentation that explains how tf.while_loop works when used with tf.Variables, and if it possible to use side_effects (like print) inside the loop, as well as indexing tensor with the loop variable ?</p>
  
  <p>Thank you in advance for your help</p>