tensorflow - Write Custom Python-Based Gradient Function for an Operation? (without C++ Implementation) -
i'm trying write custom gradient function 'my_op' sake of example contains call tf.identity() (ideally, graph).
import tensorflow tf tensorflow.python.framework import function def my_op_grad(x): return [tf.sigmoid(x)] @function.defun(a=tf.float32, python_grad_func=my_op_grad) def my_op(a): return tf.identity(a) = tf.variable(tf.constant([5., 4., 3., 2., 1.], dtype=tf.float32)) sess = tf.session() sess.run(tf.initialize_all_variables()) grad = tf.gradients(my_op(a), [a])[0] result = sess.run(grad) print(result) sess.close()
unfortunately following error:
traceback (most recent call last): file "custom_op.py", line 19, in <module> grad = tf.gradients(my_op(a), [a])[0] file "/users/njk/tfm/lib/python3.5/site-packages/tensorflow/python/framework/function.py", line 528, in __call__ return call_function(self._definition, *args, **kwargs) file "/users/njk/tfm/lib/python3.5/site-packages/tensorflow/python/framework/function.py", line 267, in call_function compute_shapes=false) file "/users/njk/tfm/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2285, in create_op raise typeerror("input #%d not tensor: %s" % (idx, a)) typeerror: input #0 not tensor: <tensorflow.python.ops.variables.variable object @ 0x1080d2710>
i know possible create custom c++ operation, in case need write custom gradient function can written in python using standard tensorflow operations, avoid writing unnecessary c++ code.
also, i'm using upstream version of tensorflow github.
note python_grad_func needs same interface ops.registergradient (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/function.py#l349).
here modified code example:
def my_op_grad(op, grad): ### instead of my_op_grad(x) return tf.sigmoid(op.inputs[0]) @function.defun(a=tf.float32, python_grad_func=my_op_grad) def my_op(a): return tf.identity(a) def main(unused_argv): = tf.variable(tf.constant([-5., 4., -3., 2., 1.], dtype=tf.float32)) sess = tf.session() sess.run(tf.initialize_all_variables()) = tf.identity(a) #workaround bug github.com/tensorflow/tensorflow/issues/3710 grad = tf.gradients(my_op(a), [a])[0] result = sess.run(grad) print(result) sess.close()
output:
[ 0.00669286 0.98201376 0.04742587 0.88079709 0.7310586 ]
Comments
Post a Comment