Tensorflow how to compute the gradient of output with respect to the input?

Viewed 3646

Recently, I try to do some experiments and I have a neural network D(x) where x is the input image with batch size 64. I want to compute the gradient of D(x) with respect to x. Should I do the computation as the following?

grad = tf.gradients(D(x), [x])

Thank you everybody!

2 Answers

During the training of a neural network, the gradient is generally computed of a loss function with respect to the input. This is because, the loss function can be well defined along with its gradient.

However, if you talk about the gradient of your output D(x), this I assume is some set of vector(s). You will need to define how the gradient will be computed with respect to its input (i.e the layer which generates the output).

The exact details of that implementation depends upon the framework which you are using.

Related