I am building a simple multi-layer perceptron with TensorFlow, and I also need to get the gradients (or error signal) of the loss on the inputs of the neural network.
Here is my code that works:
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]
(edited to include learning cycle)
Without the last line ( grads_wrt_input... ) this works very fast on a CUDA machine. However, tf.gradients() significantly reduces performance by ten times or more.
I remember that the error signals in the nodes are calculated as intermediate values ββin the backpropagation algorithm, and I have successfully done this using the Java library DeepLearning4j. I also got the impression that this would be a small modification of the calculation graph already built with optimizer .
How can this be done faster or is there any other way to calculate the loss gradients of wrt inputs?
tensorflow
Darren Foong
source share