常与tf.control_dependencies(self, control_inputs)配合使用,只有当这个上下文管理器中的是一个操作时,control_inputs才会执行。
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)
with tf.control_dependencies([x_plus_1]):
y = x
##此时程序输出会是1 1 1 1 1
##并不是一个op,需要改为y = tf.identity(x)
##程序输出为1 2 3 4 5
init = tf.global_variables_initializer()
with tf.Session() as session:
init.run()
for i in range(5):
print(y.eval())
本文介绍了TensorFlow中tf.control_dependencies的用法,通过实例演示如何确保操作按特定顺序执行。解释了为什么直接赋值不会更新变量,并展示了正确的实现方式。
1558

被折叠的 条评论
为什么被折叠?



