给定一个 TensorFlow tf.while_loop
,我如何计算每个时间步的 x_out
相对于网络所有权重的梯度?
network_input = tf.placeholder(tf.float32, [None])
steps = tf.constant(0.0)
weight_0 = tf.Variable(1.0)
layer_1 = network_input * weight_0
def condition(steps, x):
return steps <= 5
def loop(steps, x_in):
weight_1 = tf.Variable(1.0)
x_out = x_in * weight_1
steps += 1
return [steps, x_out]
_, x_final = tf.while_loop(
condition,
loop,
[steps, layer_1]
)
一些笔记
- 在我的网络中,条件是动态的。不同的运行将运行 while 循环不同的次数。
- 调用
tf.gradients(x, tf.trainable_variables())
崩溃并出现AttributeError: 'WhileContext' object has no attribute 'pred'
。似乎在循环中使用tf.gradients
的唯一可能性是计算关于weight_1
的梯度和x_in
的当前值/仅时间步长,不通过时间反向传播。 - 在每个时间步中,网络将输出 Action 的概率分布。然后需要梯度来实现策略梯度。
最佳答案
在基于this 的 Tensorflow 中,您永远不能在 tf.while_loop
中调用 tf.gradients
和 this ,当我尝试将共轭梯度下降完全创建到 Tensorflow
图中时,我发现了这一点。
但是如果我正确理解你的模型,你可以制作你自己的 RNNCell
版本并将其包装在 tf.dynamic_rnn
中,但实际的单元格
实现会有点复杂,因为您需要在运行时动态评估条件。
初学者可以看看Tensorflow的dynamic_rnn
代码here .
或者,动态图从来都不是 Tensorflow
的强项,因此请考虑使用其他框架,例如 PyTorch
或者您可以试用 eager_execution
看看是否有帮助。
关于python - 为 tf.while_loop 的每个时间步计算梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555016/