title: Tensorflow AssertionError "gradients list should have been aggregated by now"
body: <p>I have a function <code>f</code> that is internally using some <code>tf.while_loop</code>s and <code>tf.gradients</code> to compute the value <code>y = f(x)</code>. Something like this </p>
  
  <pre><code>def f( x ):
     ... 
     def body( g, x ):
          # Compute the gradient here 
          grad = tf.gradients( g, x )[0]
          ... 
          return ... 
     return tf.while_loop( cond, body, parallel_iterations=1 )
  </code></pre>
  
  <p>There are a few hundred lines of code. But I believe that those are the important points... </p>
  
  <p>Now when I evaluate <code>f(x)</code>, I get exactly the value I expect ..</p>
  
  <pre><code>y = known output of f(x)
  with tf.Session() as sess:
      fx = f(x)
      print("Error = ", y - sess.run(fx, feed_dict)) # Prints 0
  </code></pre>
  
  <p>However, when I try to evaluate the gradient of <code>f(x)</code> with respect to <code>x</code>, that is, </p>
  
  <pre><code>grads = tf.gradients( fx, x )[0]
  </code></pre>
  
  <p>I get the error </p>
  
  <pre><code>AssertionError: gradients list should have been aggregated by now.
  </code></pre>
  
  <p>Here is the full trace: </p>
  
  <pre><code>  File "C:/Dropbox/bob/tester.py", line 174, in &lt;module&gt;
      grads = tf.gradients(y, x)[0]
    File "C:\Anaconda36\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 649, in gradients
      return [_GetGrad(grads, x) for x in xs]
    File "C:\Anaconda36\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 649, in &lt;listcomp&gt;
      return [_GetGrad(grads, x) for x in xs]
    File "C:\Anaconda36\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 727, in _GetGrad
      "gradients list should have been aggregated by now.")
  AssertionError: gradients list should have been aggregated by now.
  </code></pre>
  
  <p>Could somebody please outline likely causes for this error? I have no idea where to even start looking for the issue... </p>
  
  <p>Some observations:</p>
  
  <ol>
  <li><p>Note that I have set the parallel iterations for the while loop to 1. This 
  should mean that there is no errors due to reading and writing from multiple threads. </p></li>
  <li><p>If I discard the while loop, and just have <code>f</code> return <code>body()</code>, then the code runs:</p>
  
  <pre><code># The following does not crash, but we removed the while_loop, so the output is incorrect
  def f( x ):
     ... 
     def body( g, x ):
          # Compute the gradient here 
          grad = tf.gradients( g, x )[0]
          ... 
          return ... 
     return body(...)
  </code></pre></li>
  </ol>
  
  <p>Obviously, the output is incorrect, but at least the gradients are computed. </p>