转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/83574333
本文出自【我是干勾鱼的博客】
Ingredient:
- Python:Python 3.6.6(Python Downloads)
1 功能
tf.control_dependencies()函数是用来控制计算流图的,也就是给图中的某些计算指定顺序。有的时候我们想要指定某些操作执行的依赖关系,比如想要让参数先更新,然后再获取参数更新后的值等。
tf.control_dependencies(control_inputs)会返回一个控制依赖的上下文管理器,使用了with关键字就可以让在这个上下文环境中的操作都在control_inputs 执行,比如:
with g.control_dependencies([a, b, c]):
# `d` and `e` will only run after `a`, `b`, and `c` have executed.
d = ...
e = ...
d、e的操作会在a、b、c的操作执行完之后再执行。
2 示例
比如,训练模型时有可能每步训练需要按先后顺序执行2种操作:
a
b
这时我们就可以使用如下代码:
with tf.control_dependencies([a, b]):
c= tf.no_op(name='train') #tf.no_op函数什么也不做
sess.run(c)
这会保证a、b按顺序执行。这种情况下其实可以将上面代码替换为:
c= tf.group([a, b])
sess.run(c)
3 参考
tf.control_dependencies()作用及用法
tensorflow学习笔记:control dependencies