从这里 与tf.Variable不同,tf.ResourceVariable具有明确定义的语义。 TensorFlow图中ResourceVariable的每次使用都会向图中添加read_value操作。 Tensors回归……
两个评论:
在第一个参数中写变量/ ops的顺序 sess.run 并不意味着这是执行的顺序。
sess.run
如果在一个步骤中有效的话,那并不意味着如果你添加了大量的并行性,它就会起作用。
问题的答案:
定义的关键是 depends on : a read_value operation are guaranteed to see all modifications on which the read_value depends on 。如果你看下面的图表,添加操作实际上包含一个 ReadVariableOp 操作 b , 然后 ReadVariableOp 也取决于 AssignVariableOp 。因此, c 应该考虑到所有的修改 b 。
depends on
a read_value operation are guaranteed to see all modifications on which the read_value depends on
ReadVariableOp
b
AssignVariableOp
c
除非我混合了什么,但我听起来很有说服力。 :)
如果要查看[10.0,5.0,0.0],则必须添加 tf.control_dependency 如下
tf.control_dependency
tf.reset_default_graph() a = tf.placeholder(dtype=tf.float32,shape=(), name='a') d = tf.placeholder(dtype=tf.float32,shape=(), name='d') b = tf.get_variable(name='b', initializer=tf.zeros_like(d), use_resource=True) c=a+b with tf.control_dependencies([c]): b_init = tf.assign(b, d) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run([b_init,c,b], feed_dict={a:5.,d:10.}))
然后图表会稍微改变一下