tf.gradients
语法
tf.gradients(ys,
xs,
grad_ys=None,
name='gradients',
colocate_gradients_with_ops=False,
gate_gradients=False,
aggregation_method=None,
stop_gradients=None)
tf.gradients(
ys,
xs,
grad_ys=None,
name='gradients',
gate_gradients=False,
aggregation_method=None,
stop_gradients=None,
unconnected_gradients=tf.UnconnectedGradients.NONE
)
含义
ys对xs进行求导
[
∂
y
∂
x
]
\left[\frac{\partial y}{\partial x}\right]
[∂x∂y]
[ ∂ y s ∂ x s ] \left[\frac{\partial ys}{\partial xs}\right] [∂xs∂ys]
示例
self.a_grads = tf.gradients(self.a_loss, self.a_params)
self.c_grads = tf.gradients(self.c_loss, self.c_params)
Actor网络的损失函数a_loss关于Actor网络参数a_params求梯度:
ys1 = a_loss
xs1 = a_params
Critic网络的损失函数c_loss关于Critic网络参数c_params求梯度:
ys2 = c_loss
xs2 = c_params
https://tensorflow.google.cn/api_docs/python/tf/gradients?hl=en
∣
∂
l
o
s
s
∂
l
o
c
a
l
_
q
]
\left|\frac{\partial\mathrm{loss}}{\partial\mathrm{local}\_\mathrm{q}}\right]
∂local_q∂loss]
q_gradients = tf.gradients(loss, local_q)