Skip to content Skip to sidebar Skip to footer

How To Register A Custom Gradient For A Operation Composed Of Tf Operations

More specifically I have a simple fprop that is a composition of tf operations. I want to override the tensorflow gradient computation with my own gradient method using RegisterGra

Solution 1:

You need to define the op within the scope of with g.gradient_override_map({'Myop': 'MyopGrad'})

Also, you need to map Identity rather than the name Myop to your new gradient.

Here is the full code:

import tensorflow as tf
from tensorflow.python.framework import ops

@ops.RegisterGradient("MyopGrad")
def frop_grad(op, grad):
    x = op.inputs[0]
    return 0 * x  # zero out to see the difference:

def fprop(x):
    x = tf.sqrt(x)
    out = tf.maximum(x, .2)
    return out

a = tf.Variable(tf.constant([5., 4., 3., 2., 1.], dtype=tf.float32))
h = fprop(a)

g = tf.get_default_graph()
with g.gradient_override_map({'Identity': 'MyopGrad'}):
    h = tf.identity(h, name="Myop")
    grad = tf.gradients(h, a)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    result = sess.run(grad)

print(result[0])

Output:

[ 0.  0.  0.  0.  0.]

Solution 2:

If you want to use tf.RegisterGradient() for this purpose, I'm not sure if it is a proper solution. Because in the official documents https://www.tensorflow.org/api_docs/python/tf/RegisterGradient , it says:

This decorator is only used when defining a new op type.

which means you need to define a new op written in C++ or wrapped in py_func. I'm not totally sure if it can apply on the group of "tf op" you said.


However, You can also refer to the "trick" methods mentioned in this thread:

How Can I Define Only the Gradient for a Tensorflow Subgraph?

where you could combine tf.stop_gradient() and tfgradient_override_map() together to re-define the gradients for groups of operations


Solution 3:

See this answer (note that different questions might be satisfactorily answered by the same answer).


Post a Comment for "How To Register A Custom Gradient For A Operation Composed Of Tf Operations"