Guest User

Untitled

a guest
Dec 11th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import tensorflow as tf
  2. from tensorflow.python.framework import ops
  3. from tensorflow.python.ops import gen_nn_ops
  4.  
  5. @ops.RegisterGradient("GuidedRelu")
  6. def _GuidedReluGrad(op, grad):
  7. return tf.select(0. < grad, gen_nn_ops._relu_grad(grad, op.outputs[0]), tf.zeros(grad.get_shape()))
  8.  
  9. if __name__ == '__main__':
  10. with tf.Session() as sess:
  11. g = tf.get_default_graph()
  12. x = tf.constant([10., 2.])
  13. with g.gradient_override_map({'Relu': 'GuidedRelu'}):
  14. y = tf.nn.relu(x)
  15. z = tf.reduce_sum(-y ** 2)
  16. tf.initialize_all_variables().run()
  17.  
  18. print x.eval(), y.eval(), z.eval(), tf.gradients(z, x)[0].eval()
  19. # > [ 10. 2.] [ 10. 2.] -104.0 [ 0. 0.]
Add Comment
Please, Sign In to add comment