【问题标题】:Implement Guided BackProp in TensorFlow 2.0?在 TensorFlow 2.0 中实现 Guided BackProp?
【发布时间】: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


【解决方案1】:

在 tf2.0/2.1 中似乎没有一个干净的方法。我使用的解决方法是通过使用@custom_gradient 的自定义 ReLU 更改 ReLU 来修改我的模型。我受到this thread 的启发。它有点慢,但至少它有效。 TF 肯定会再次更新以支持重新映射渐变。同时希望对您有所帮助。

编辑:问题讨论该问题here

【讨论】:

  • 谢谢!我猜基于this answer 你的方法是推荐的。
猜你喜欢
  • 2019-09-19
  • 1970-01-01
  • 2020-06-12
  • 2019-12-16
  • 2020-10-13
  • 2020-03-09
  • 2020-07-24
  • 2020-01-05
  • 1970-01-01
相关资源
最近更新 更多