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

标签 python python-2.7 tensorflow

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

但是,我遇到了几个错误,除了一个我可以修复。 在代码中,使用了作为 tf.Variable 成员函数的 ref()(12),但在最近的 tensorflow 版本 0.12rc 中,该函数似乎已被弃用。 所以我不知道替换它的最佳方式是什么(我不明白作者为什么使用ref())。当我只是把它改成变量本身时(比如v.ref()改成v),没有报错,但是reward没有变。它似乎无法学习,我猜这是因为变量没有正确更新。

请告诉我修改代码以使其工作的正确方法是什么。

最佳答案

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

此方法的用例解释起来有点棘手,它的动机是某些缓存行为,该行为会导致在不同设备上多次使用远程变量以使用缓存值。假设您有以下代码:

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)

您可以使用 tf.Variable.read_value() 强制 TensorFlow 稍后再次读取该变量,它会受到任何控制依赖项的影响。所以如果你想在计算 m2 时看到赋值的结果,你可以修改程序的最后一个 block ,如下所示:

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(), ...)

(请注意,目前,如果所有操作都在同一台设备上,您不需要使用read_value(),因为 TensorFlow 不当变量用作同一设备上操作的输入时,复制该变量。这可能会导致很多困惑——例如,当你将一个变量排入队列时!——这也是我们“正在致力于增强变量的内存模型。)

关于python - Tensorflow 版本 0.12 中 tf.Variable.ref() 的替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901391/

相关文章:

python Tkinter 多线程

python - 我可以在字典中包含 3 个元素吗

python - 用 Python 发牌?

mysql - Python-Sqlalchemy 二进制列类型 HEX() 和 UNHEX()

Python - 评估列表子集的元素

python - 如何使用机器学习从数据中检测英语单词

python - 如何在 tensorflow 中打乱 3D 张量维度?

python - 编写领域特定语言以从表中选择行

python - 将 Docx 转换为纯文本

python - tensorflow.python.framework.errors_impl.NotFoundError : Op type not registered 'FertileStatsResourceHandleOp' in binary running on NS80011718