您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为操作编写基于Python的自定义渐变函数?(没有C ++实现)

为操作编写基于Python的自定义渐变函数?(没有C ++实现)

请注意python_grad_func需要与ops.RegisterGradient(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/function.py#L349)相同的接口。

这是修改后的代码示例:

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()

输出

[ 0.00669286  0.98201376  0.04742587  0.88079709  0.7310586 ]
python 2022/1/1 18:39:10 有246人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶