为什么custom_gradient在计算图中添加了Identity运算符?

时间:2018-11-20 18:36:43

标签: tensorflow

我正在尝试了解反向模式autodiff和Tensorflow的autodiff算法。

对于以下代码,渐变图看起来与我预期的一样:

f = tf.reduce_sum
g = tf.log
p = tf.sin
w = tf.get_variable('w', [1,2])
x = p(w, name='x')
y = g(x, name='y')
z = f(y, name='z')
grads = tf.gradients(z,w)

enter image description here

但是,当我使用tf.custom_gradient时,图形上还有一个额外的IdentityN运算符。

@tf.custom_gradient
def myOP(w):
    x = tf.sin(w, name='x')
    def grad(dz):
        return dz*x
    return x, grad

f = tf.reduce_sum
g = tf.log
p = myOP
w = tf.get_variable('w', [1,2])
x = p(w)
y = g(x, name='y')
z = f(y, name='z')

grads = tf.gradients(z,w)

enter image description here

您能否解释一下这个额外的IdentityN运算符的作用以及为什么要添加它?

0 个答案:

没有答案