Tensorflow:什么时候用列表在 sess.run 中完成变量赋值? [英] Tensorflow: When are variable assignments done in sess.run with a list?
问题描述
我认为变量赋值是在给 sess.run 的列表中的所有操作之后完成的,但是下面的代码在不同的执行中返回不同的结果.似乎随机运行列表中的操作,并在列表中的操作运行后分配变量.
I have thought that variable assignments are done after all operations in a list given to sess.run, but the following code returns different results at different execution. It seems randomly run operations in the list and assign the variable after the run of the operation in the list.
a = tf.Variable(0)
b = tf.Variable(1)
c = tf.Variable(1)
update_a = tf.assign(a, b + c)
update_b = tf.assign(b, c + a)
update_c = tf.assign(c, a + b)
with tf.Session() as sess:
sess.run(initialize_all_variables)
for i in range(5):
a_, b_, c_ = sess.run([update_a, update_b, update_c])
我想知道变量赋值的时间.哪些是正确的:update_x -> 分配 x -> ... -> udpate_z -> 分配 z"或update_x -> udpate_y -> udpate_z -> 分配 a、b、c"?(其中(x,y,z)是(a,b,c)的排列)另外,如果有实现后一个赋值的方法(列表中的所有操作完成后赋值),请告诉我如何实现.
I'd like to know the timing of variable assignments. Which are correct: "update_x -> assign x -> ... -> udpate_z -> assign z" or "update_x -> udpate_y -> udpate_z -> assign a, b, c"? (where (x, y, z) is a permutation of (a, b, c)) In addition, if there is a way that realize the latter assignment (assignment are done after all operations in the list are done), please let me know how to realize it.
推荐答案
update_a
、update_b
和 update_c
三个操作没有相互依赖在数据流图中,因此 TensorFlow 可以选择以任何顺序执行它们.(在当前的实现中,它们三个可能会在不同的线程上并行执行.)第二个 nit 是默认情况下缓存变量的读取,因此在您的程序中,update_b<中分配的值/code>(即
c + a
)可能会使用a
的原始值或更新后的值,这取决于第一次读取变量的时间.
The three operations update_a
, update_b
, and update_c
have no interdependencies in the dataflow graph, so TensorFlow may choose to execute them in any order. (In the current implementation, it is possible that all three of them will be executed in parallel on different threads.) A second nit is that reads of variables are cached by default, so in your program the value assigned in update_b
(i.e. c + a
) may use the original or the updated value of a
, depending on when the variable is first read.
如果您想确保操作按特定顺序发生,您可以使用 with tf.control_dependencies([...]):
块以强制在块中创建的操作发生在列表中指定的操作之后.您可以使用 tf.Variable.read_value()
在带有 tf.control_dependencies([...]): 的 块中,以明确读取变量的位置.
If you want to ensure that the operations happen in a particular order, you can use with tf.control_dependencies([...]):
blocks to enforce that operations created within the block happen after operations named in the list. You can use tf.Variable.read_value()
inside a with tf.control_dependencies([...]):
block to make the point at which the variable is read explicit.
因此,如果您想确保 update_a
在 update_b
之前发生并且 update_b
在 update_c
之前发生,你可以这样做:
Therefore, to if you want to ensure that update_a
happens before update_b
and update_b
happens before update_c
, you could do:
update_a = tf.assign(a, b + c)
with tf.control_dependencies([update_a]):
update_b = tf.assign(b, c + a.read_value())
with tf.control_dependencies([update_b]):
update_c = tf.assign(c, a.read_value() + b.read_value())
这篇关于Tensorflow:什么时候用列表在 sess.run 中完成变量赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!