Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow版本0.12中tf.Variable.ref()的替代方案是什么?_Python_Python 2.7_Tensorflow - Fatal编程技术网

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

Python Tensorflow版本0.12中tf.Variable.ref()的替代方案是什么?,python,python-2.7,tensorflow,Python,Python 2.7,Tensorflow,我正在尝试运行A3C强化学习算法的开放代码来学习A3C 然而,我有几个错误,我可以修复,除了一个。 在代码中,使用了tf.Variable的成员函数(,)的ref(),但在最近的tensorflow版本0.12rc中,该函数似乎被弃用。 因此,我不知道什么是替换它的最佳方法(我不清楚作者为什么使用ref())。当我刚刚将其更改为变量本身(例如v.ref()更改为v)时,没有出现错误,但奖励没有更改。它似乎无法学习,我猜这是因为变量没有正确更新 请告诉我修改代码的正确方法 新方法取代了Tensor

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

然而,我有几个错误,我可以修复,除了一个。 在代码中,使用了tf.Variable的成员函数(,)的
ref()
,但在最近的tensorflow版本0.12rc中,该函数似乎被弃用。 因此,我不知道什么是替换它的最佳方法(我不清楚作者为什么使用
ref()
)。当我刚刚将其更改为变量本身(例如
v.ref()
更改为
v
)时,没有出现错误,但奖励没有更改。它似乎无法学习,我猜这是因为变量没有正确更新

请告诉我修改代码的正确方法

新方法取代了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
时看到赋值的结果,您可以修改程序的最后一块,如下所示:

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在同一设备上用作op的输入时不会复制变量。这可能会导致很多混乱,例如,当您将变量排入队列时!-这也是我们致力于增强变量内存模型的原因之一。)

新方法取代了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
时看到赋值的结果,您可以修改程序的最后一块,如下所示:

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在同一设备上用作op的输入时不会复制变量。这可能会导致很多混乱,例如,当您将变量排入队列时!-这也是我们致力于增强变量内存模型的原因之一。)

非常感谢您快速而详细的回答。这是非常翔实的,我可以很好地理解。非常感谢您的快速和详细的回答。这本书内容丰富,我能很好地理解。