Last active
April 1, 2021 09:12
-
-
Save falcondai/561d5eec7fed9ebf48751d124a77b087 to your computer and use it in GitHub Desktop.
Tensorflow implementation of guided backpropagation through ReLU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow.python.framework import ops | |
from tensorflow.python.ops import gen_nn_ops | |
@ops.RegisterGradient("GuidedRelu") | |
def _GuidedReluGrad(op, grad): | |
return tf.select(0. < grad, gen_nn_ops._relu_grad(grad, op.outputs[0]), tf.zeros(grad.get_shape())) | |
if __name__ == '__main__': | |
with tf.Session() as sess: | |
g = tf.get_default_graph() | |
x = tf.constant([10., 2.]) | |
with g.gradient_override_map({'Relu': 'GuidedRelu'}): | |
y = tf.nn.relu(x) | |
z = tf.reduce_sum(-y ** 2) | |
tf.initialize_all_variables().run() | |
print x.eval(), y.eval(), z.eval(), tf.gradients(z, x)[0].eval() | |
# > [ 10. 2.] [ 10. 2.] -104.0 [ 0. 0.] |
How is it possible to compute GuidedGrad for other activations functions which their gradients are not included in gen_nn_ops
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works after replacing tf.select by tf.where for Tensorflow 1.2
Thank you @facondai