title: Weird behaviour of back propagation in a while_loop in Tensorflow
body: <p>I am trying to see if Tensorflow can backpropagate through a while_loop without a pre-defined number of loops. Something weird caught my attention:</p>
  
  <pre><code>import tensorflow as tf
  from numpy import *
  
  tf.reset_default_graph()
  
  X = tf.Variable(5.0, dtype = float32)
  Y = tf.Variable(4.0, dtype = float32)
  
  def cond(iterN, Y):
      return iterN&lt;6
  
  def body(iterN, X):
      return iterN+1, tf.add(X,X)
      # return iterN+1, tf.multiply(X,2)
  
  finalOutX = tf.while_loop(cond, body, [0,X], parallel_iterations=1, back_prop=True, swap_memory=True)
  
  gra = tf.gradients(finalOutX, X)
  
  init = tf.global_variables_initializer()
  
  with tf.Session() as sess:
  
      init.run()
      X_val, graVal, = sess.run([finalOutX, gra])    
  
  print(X_val)
  print(graVal)
  </code></pre>
  
  <p>When the <code>body(iterN, X)</code> function returns <code>iterN+1</code>, <code>tf.add(X,X)</code>, I got an error stating </p>
  
  <pre><code>InvalidArgumentError: Retval[0] does not have value
  </code></pre>
  
  <p>However, when I replaced <code>tf.add(X,X)</code> by <code>tf.multiply(X,2)</code>, the code can be executed and return the correct answer <code>gra=64</code>. </p>
  
  <p>Could anyone help me to understand where the error came from when I was using <code>tf.add(X,X)</code>? Thanks in advance!</p>