如何访问 Tensorflow 中循环单元的权重?

How can I access the weights of a recurrent cell in Tensorflow?

提高深度 Q 学习任务稳定性的一种方法是为网络维护一组更新缓慢的目标权重,用于计算 Q 值目标。作为学习过程中不同时间的结果,前向传递中使用了两组不同的权重。对于普通的 DQN,这并不难实现,因为权重是可以在 feed_dict 中设置的张量流变量,即:

sess = tf.Session()
input = tf.placeholder(tf.float32, shape=[None, 5])
weights = tf.Variable(tf.random_normal(shape=[5,4], stddev=0.1)
bias = tf.Variable(tf.constant(0.1, shape=[4])
output = tf.matmul(input, weights) + bias
target = tf.placeholder(tf.float32, [None, 4])
loss = ...

...

#Here we explicitly set weights to be the slowly updated target weights
sess.run(output, feed_dict={input: states, weights: target_weights, bias: target_bias})

# Targets for the learning procedure are computed using this output.

....

#Now we run the learning procedure, using the most up to date weights,
#as well as the previously computed targets
sess.run(loss, feed_dict={input: states, target: targets})

我想在 DQN 的循环版本中使用这种目标网络技术,但我不知道如何访问和设置循环单元中使用的权重。具体来说,我使用的是 tf.nn.rnn_cell.BasicLSTMCell,但我想知道如何对任何类型的循环单元执行此操作。

BasicLSTMCell 不会将其变量作为其 public API 的一部分公开。我建议您查看这些变量在图表中的名称并提供这些名称(这些名称不太可能更改,因为它们在检查点中,更改这些名称会破坏检查点兼容性)。

或者,您可以制作 BasicLSTMCell 的副本,它会公开变量。我认为这是最干净的方法。

您可以使用下面的行来获取图中的变量

variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)

然后您可以检查这些变量,看看它们是如何变化的