(备忘录)gradient_override_map 可能的用法
gradient_override_map 用法
网上也比较乱,我只是把测试的结果内容发出来,以备以后查看。
代码:
import tensorflow as tf
import math
from tensorflow.python.training import moving_averages
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.framework import ops
def tenary_yunsaun(x):
return
def tenary_opration(x):
g = tf.get_default_graph()
with ops.name_scope("tenarized") as name:
with g.gradient_override_map({"Sign": "Identity"}):
return tf.sign(tf.add(tf.sign(tf.add(x,0.9)),tf.sign(tf.add(x,-0.9))))
#test
x=tf.Variable([-9,-4,0.5,1,-2,-0.9,-3.4,1.5,11,-7.2])
y=tenary_opration(x)
grad=tf.gradients(y,x)
with tf.Session() as sess:
tf.global_variables_initializer().run()
y=sess.run(y)
print "----------------------"
print "y=" + str(y)
print sess.run(grad)
结果
y=[-1. -1. 0. 1. -1. -1. -1. 1. 1. -1.]
[array([ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=float32)]