Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tensorflow

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.

like image 227
Daniel Slater Avatar asked Feb 05 '16 14:02

Daniel Slater


People also ask

How does TensorFlow calculate gradient?

TensorFlow "records" relevant operations executed inside the context of a tf. GradientTape onto a "tape". TensorFlow then uses that tape to compute the gradients of a "recorded" computation using reverse mode differentiation.

What is a gradient in TensorFlow?

The gradients are the partial derivatives of the loss with respect to each of the six variables. TensorFlow presents the gradient and the variable of which it is the gradient, as members of a tuple inside a list. We display the shapes of each of the gradients and variables to check that is actually the case.

How do you find the value of TensorFlow variable?

To get the current value of a variable x in TensorFlow 2, you can simply print it with print(x) . This prints a representation of the tf. Variable object that also shows you its current value.


2 Answers

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: ...}) 
like image 136
mrry Avatar answered Sep 20 '22 21:09

mrry


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 
like image 39
thushv89 Avatar answered Sep 19 '22 21:09

thushv89