7
更具體我有一個簡單fprop即TF操作的複合操作。 我想用我自己的梯度法使用RegisterGradient來重寫張量梯度計算。如何註冊自定義梯度爲TF操作組成
有什麼不對的代碼?
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)
h = tf.identity(h, name="Myop")
grad = tf.gradients(h, a)
g = tf.get_default_graph()
with g.gradient_override_map({'Myop': 'MyopGrad'}):
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
result = sess.run(grad)
print(result[0])
我想看看在打印全部爲零,而是我得到:
[ 0.2236068 0.25000003 0.28867513 0.35355341 0.5 ]
不這樣定義的身份運算,而不是fprop功能的自定義功能梯度?如果你不乘以零乘你不會看到[5.,4.,3.,2.,1.],而是你會看到身份()op的輸入。 – Milad