title: TensorFlow: slow performance when getting gradients at inputs
body: <p>I'm building a simple multilayer perceptron with TensorFlow, and I also need to obtain the gradients (or error signal) of the loss at the neural network's inputs.</p>
  
  <p>Here's my code, which works:</p>
  
  <pre><code>cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.network, self.y))
  optimizer = tf.train.AdagradOptimizer(learning_rate=nn_learning_rate).minimize(cost)
  ...
  for i in range(epochs):
      ....
      for batch in batches:
          ...
          sess.run(optimizer, feed_dict=feed_dict)
          grads_wrt_input = sess.run(tf.gradients(cost, self.x), feed_dict=feed_dict)[0]
  </code></pre>
  
  <p>(edited to include training loop)</p>
  
  <p>Without the last line (<code>grads_wrt_input...</code>), this runs really fast on a CUDA machine. However, <code>tf.gradients()</code> reduces performance greatly by tenfold or more.</p>
  
  <p>I recall that the error signals at the nodes are computed as intermediate values in the backpropagation algorithm, and I have successfully done this using the Java library DeepLearning4j. I was also under the impression that this would be a slight modification to the computation graph already built by <code>optimizer</code>.</p>
  
  <p>How can this be made faster, or is there any other way to compute the gradients of the loss w.r.t. the inputs?</p>