Graph Tensorflow:tf.图的不同路径之间的梯度

Graph Tensorflow:tf.图的不同路径之间的梯度,graph,tensorflow,reinforcement-learning,gradient,Graph,Tensorflow,Reinforcement Learning,Gradient,我正在开发一个DDPG实现,它需要计算一个网络(低于:critic)相对于另一个网络(低于:actor)输出的梯度。我的代码已经在大部分情况下使用了队列而不是提要dict,但是我还不能在这个特定部分这样做: import tensorflow as tf tf.reset_default_graph() states = tf.placeholder(tf.float32, (None,)) actions = tf.placeholder(tf.float32, (None,)) acto

我正在开发一个DDPG实现,它需要计算一个网络(低于:
critic
)相对于另一个网络(低于:
actor
)输出的梯度。我的代码已经在大部分情况下使用了队列而不是提要dict,但是我还不能在这个特定部分这样做:

import tensorflow as tf
tf.reset_default_graph()

states = tf.placeholder(tf.float32, (None,))
actions = tf.placeholder(tf.float32, (None,))

actor = states * 1
critic = states * 1 + actions

grads_indirect = tf.gradients(critic, actions)
grads_direct = tf.gradients(critic, actor)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    act = sess.run(actor, {states: [1.]})
    print(act)  # -> [1.]
    cri = sess.run(critic, {states: [1.], actions: [2.]})
    print(cri)  # -> [3.]
    grad1 = sess.run(grads_indirect, {states: [1.], actions: act})
    print(grad1)  # -> [[1.]]
    grad2 = sess.run(grads_direct, {states: [1.], actions: [2.]})
    print(grad2)  # -> TypeError: Fetch argument has invalid type 'NoneType'
grad1
此处计算到fed in动作的梯度w.r.t.,该梯度先前由
参与者计算
grad2
也应该这样做,但是直接在图形内部,不需要反馈操作,而是直接评估
actor
。问题是,
grads\u direct
None

print(grads_direct)  # [None]

我怎样才能做到这一点?有没有专门的“评估这个张量”操作我可以使用?谢谢

在您的示例中,您没有使用
actor
来计算
critic
,因此梯度为无

你应该做:

actor = states * 1
critic = actor + actions  # change here

grads_indirect = tf.gradients(critic, actions)
grads_direct = tf.gradients(critic, actor)