您需要在以下范围内定义 *** 作
with g.gradient_override_map({'Myop': 'MyopGrad'})
另外,您需要映射
Identity而不是名称
Myop到新渐变。
这是完整的代码:
import tensorflow as tffrom 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 outa = 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])
输出:
[ 0. 0. 0. 0. 0.]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)