1
나는 들어오는 그라데이션을 두 배로하는 커스텀 그라디언트 계산 기능을 가지고 있습니다. 내가 할TensorFlow 커스텀 그라디언트
import tensorflow as tf
@tf.RegisterGradient("CustomSquare")
def _custom_square_grad(op, grad):
return grad*2.0
c = tf.constant(3.)
s1 = tf.square(c)
grad1 = tf.gradients(s1, c)[0]
g = tf.get_default_graph()
with g.gradient_override_map({"Square": "CustomSquare"}):
s2 = tf.square(c)
grad2 = tf.gradients(s2, c)[0]
with tf.Session() as sess:
print(sess.run([c, s1, grad1]))
print(sess.run([c, s2, grad2]))
결과는 놀라운 일이다 : 나는 두 번째 결과를 기다리고 있었다
[3.0, 9.0, 6.0]
[3.0, 9.0, 2.0]
[3.0, 9.0, 12.0]
될 수 있습니다. 내가 뭘 놓치고 있니?
감사합니다.