【发布时间】:2019-10-28 15:20:55
【问题描述】:
我正在尝试使用 TensorFlow 2.0 应用引导式反向传播 (https://arxiv.org/abs/1412.6806)。要应用引导反向传播,我们需要修改 relu 梯度。 我阅读了How to apply Guided BackProp in Tensorflow 2.0? 中的对话,并尝试从 https://gist.github.com/falcondai/561d5eec7fed9ebf48751d124a77b087,但是结果并不如我所料。我不确定我错过了什么。
这是我所拥有的(结合以上来源的代码):
import tensorflow as tf
@tf.RegisterGradient("GuidedRelu")
def _GuidedReluGrad(op, grad):
dtype = op.inputs[0].dtype
gate_f = tf.cast(op.outputs[0] > 0, dtype) #for f^l > 0
gate_R = tf.cast(grad > 0, dtype) #for R^l+1 > 0
return gate_f * gate_R * grad
with tf.compat.v1.get_default_graph().gradient_override_map({'Relu': 'GuidedRelu'}):
with tf.GradientTape() as tape:
x = tf.constant([10., 2.])
tape.watch(x)
y = tf.nn.relu(x)
z = tf.reduce_sum(-y ** 2)
print(x.numpy())
print(y.numpy())
print(z.numpy())
print(tape.gradient(z, x).numpy())
输出是
[10. 2.]
[10. 2.]
-103.99999
[-20. -4.]
代替
[10. 2.]
[10. 2.]
-103.99999
[0. 0.]
【问题讨论】:
-
您好,我也遇到了同样的问题。你解决了吗?
-
嗨,我最终不再研究这个主题了,所以我没有关于它的更新。也许下面的@L3Robot 回答会有所帮助,尤其是this comment。
标签: python tensorflow relu