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

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: ...})

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)