Tensorflow版本0.12中tf.Variable.ref()的替代方法是什么?

use*_*700 1 python python-2.7 tensorflow

我正在尝试运行A3C强化学习算法的开放代码来学习A3C代码中的A3C

但是,我遇到了几个错误,除了一个,我可以解决.中的代码,ref()其是被用来tf.Variable的成员函数(1,2),但在最近tensorflow版本0.12rc,该功能似乎被弃用.所以我不知道替换它的最佳方法是什么(我不明白为什么作者使用了ref()).当我刚将它改为变量本身(例如v.ref()to v)时,没有错误,但奖励没有改变.它似乎无法学习,我想这是因为变量没有正确更新.

请告诉我修改代码的正确方法是什么.

mrr*_*rry 6

新方法tf.Variable.read_value()tf.Variable.ref()TensorFlow 0.12及更高版本的替代品.

此方法的用例有点难以解释,并且受到某些缓存行为的激励,这些缓存行为导致在不同设备上多次使用远程变量来使用缓存值.假设您有以下代码:

with tf.device("/cpu:0")
  v = tf.Variable([[1.]])

with tf.device("/gpu:0")
  # The value of `v` will be captured at this point and cached until `m2`
  # is computed.
  m1 = tf.matmul(v, ...)

with tf.control_dependencies([m1])
  # The assign happens (on the GPU) after `m1`, but before `m2` is computed.
  assign_op = v.assign([[2.]])

with tf.control_dependencies([assign_op]):
  with tf.device("/gpu:0"):
    # The initially read value of `v` (i.e. [[1.]]) will be used here,
    # even though `m2` is computed after the assign.
    m2 = tf.matmul(v, ...)

sess.run(m2)
Run Code Online (Sandbox Code Playgroud)

您可以使用tf.Variable.read_value()强制TensorFlow稍后再次读取变量,它将受制于任何控制依赖项.因此,如果您想在计算时看到assign的结果m2,您将修改程序的最后一个块,如下所示:

with tf.control_dependencies([assign_op]):
  with tf.device("/gpu:0"):
    # The `read_value()` call will cause TensorFlow to transfer the
    # new value of `v` from the CPU to the GPU before computing `m2`.
    m2 = tf.matmul(v.read_value(), ...)
Run Code Online (Sandbox Code Playgroud)

(注意,目前,如果所有操作都在同一设备上,则不需要使用read_value(),因为当TensorFlow用作同一设备上op的输入时,它不会复制该变量这会引起很多混淆 - 例如,当你将一个变量排入队列时! - 这也是我们正在努力增强变量内存模型的原因之一.)