title: No gradients provided for any variable for increasing Loss value inside tf.while_loop
body: <p>I have a CNN architecture, with consists of some layers -- convolution, fully-connected, and deconvolution -- (called it with <strong>first process</strong>). The last deconvolution layer gives me the points as the output and I need to do some processing (call it with <strong>second process</strong>) with this output to get the <code>Loss</code> value. </p>
  
  <p>In the <strong>second process</strong>, I'm doing the <code>tf.while_loop</code> for calculating the <code>Loss</code> value, because the <code>Loss</code> value is achieved by adding all <code>Loss</code> values from each iteration in <code>tf.while_loop</code>. And I give the <code>tf.constant(0)</code> for the <code>Loss</code> initialization before looping.</p>
  
  <p>When I tried to train and minimize that <code>Loss</code>, it shows me the error of <strong>No gradient provided</strong> between the output from <strong>first process</strong> and <strong>Loss tensor</strong>.</p>
  
  <p>The <strong>second process</strong> looks like this:</p>
  
  <pre><code>loss = tf.constant(0)
  i = tf.constant(0)
  
  def cond(i, loss):
      return tf.less(i, tf.size(xy))
  
  def body(i, loss):
      # xy is the output from the first process
      xy = tf.cast(xy, tf.float32)
      x = tf.reduce_mean(xy) 
      loss = tf.add(loss, x)
      return [tf.add(i,1), loss]
  r = tf.while_loop(cond, body, [i, loss])
  
  optimizer.minimize(r[1])
  </code></pre>
  
  <p>And I also do some processing inside the <strong>second process</strong> which (I read from many posts, especially <a href="https://github.com/tensorflow/tensorflow/issues/1511" rel="nofollow noreferrer">here</a>) doesn't provide gradient. </p>
  
  <p>Any help would be really appreciated.</p>