Write Custom Python-Based Gradient Function for an Operation? (without C++ Implementation)

前端 未结 2 1805
遥遥无期
遥遥无期 2021-01-03 02:53

I\'m trying to write a custom gradient function for \'my_op\' which for the sake of the example contains just a call to tf.identity() (ideally, it could be any graph).

2条回答
  •  Happy的楠姐
    2021-01-03 03:22

    Note that python_grad_func needs the same interface as ops.RegisterGradient (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/function.py#L349).

    Here is the 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):                                                           
    
      a = tf.Variable(tf.constant([-5., 4., -3., 2., 1.], dtype=tf.float32))         
      sess = tf.Session()                                                            
      sess.run(tf.initialize_all_variables())                                        
    
      a = tf.identity(a) #workaround for 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 ]
    

提交回复
热议问题