How do I get the gradient of the loss at a TensorFlow variable?

The feature I'm after is to be able to tell what the gradient of a given variable is with respect to my error function given some data.

One way to do this would be to see how much the variable has changed after a call to train, but obviously that can vary massively based on the learning algorithm (for example it would be almost impossible to tell with something like RProp) and just isn't very clean.

Thanks in advance.


The tf.gradients() function allows you to compute the symbolic gradient of one tensor with respect to one or more other tensors—including variables. Consider the following simple example:

data = tf.placeholder(tf.float32)
var = tf.Variable(...)              # Must be a tf.float32 or tf.float64 variable.
loss = some_function_of(var, data)  # some_function_of() returns a `Tensor`.

var_grad = tf.gradients(loss, [var])[0]

You can then use this symbolic gradient to evaluate the gradient in some specific point (data):

sess = tf.Session()

var_grad_val = sess.run(var_grad, feed_dict={data: ...})

In TensorFlow 2.0 you can use GradientTape to achieve this. GradientTape records the gradients of any computation that happens in the context of that. Below is an example of how you might do that.

import tensorflow as tf

# Here goes the neural network weights as tf.Variable
x = tf.Variable(3.0)

# TensorFlow operations executed within the context of
# a GradientTape are  recorded for differentiation 
with tf.GradientTape() as tape:
  # Doing the computation in the context of the gradient tape
  # For example computing loss
  y = x ** 2 

# Getting the gradient of network weights w.r.t. loss
dy_dx = tape.gradient(y, x) 
print(dy_dx)  # Returns 6