stop_gradient法
# Input (s, a), output q
self.a = tf.stop_gradient(a) # stop critic update flows to actor,就是说a可以参与正向计算,不能参与反向传播
self.q = self._build_net(S, self.a, 'eval_net', trainable=True)
对于自定义梯度,还有一种比较简洁的操作,就是利用tf.stop_gradient()函数,我们看下例子[1]:
t = g(x)
y = t + tf.stop_gradient(f(x) - t)
这里,我们本来的前向传递函数是f(x),但是想要在反向时传递的函数是g(x),因为在前向过程中,tf.stop_gradient()不起作用,因此+t和-t抵消掉了,只剩下f(x)前向传递;而在反向过程中,因为tf.stop_gradient()的作用,使得f(x)-t的梯度变为了0,从而只剩下g(x)在反向传递。
我们看下完整的例子:
import tensorflow as tf
x1 = tf.Variable(1)
x2 = tf.Variable(3)
x3 = tf.Variable(6)
f = x1+x2*x3
t = -f
y1 = t + tf.stop_gradient(f-t)
y2 = f
grad_1 = tf.gradients(y1, x1)
grad_2 = tf.gradients(y2, x1)
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(grad_1))
print(sess.run(grad_2))
---------------------
作者:FesianXu
来源:CSDN
原文:https://blog.csdn.net/LoseInVain/article/details/83108001
版权声明:本文为博主原创文章,转载请附上博文链接!