您的位置:首页 > 其它

tf.control_dependencies()

2018-04-02 19:36 537 查看
tf.control_dependencies()
设计是用来控制计算流图的,给图中的某些计算指定顺序。比如:我们想要获取参数更新后的值,那么我们可以这么组织我们的代码。
opt = tf.train.Optimizer().minize(loss)

with tf.control_dependencies([opt]):
updated_weight = tf.identity(weight)

with tf.Session() as sess:
tf.global_variables_initializer().run()
sess.run(updated_weight, feed_dict={...}) # 这样每次得到的都是更新后的weight
关于tf.control_dependencies的具体用法,請移步官网https://www.tensorflow.org/api_docs/python/tf/Graph#control_dependencies,总结一句话就是,在执行某些
op,tensor
之前,某些
op,tensor
得首先被运行。

下面说明两种 control_dependencies 不 work 的情况

下面有两种情况,control_dependencies不work,其实并不是它真的不work,而是我们的使用方法有问题。
第一种情况:
import tensorflow as tf
w = tf.Variable(1.0)
ema = tf.train.ExponentialMovingAverage(0.9)
update = tf.assign_add(w, 1.0)

ema_op = ema.apply([update])
with tf.control_dependencies([ema_op]):
ema_val = ema.average(update)

with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(3):
print(sess.run([ema_val]))
也许你会觉得,在我们
sess.run([ema_val])
ema_op
都会被先执行,然后再计算
ema_val
,实际情况并不是这样,为什么?
有兴趣的可以看一下源码,就会发现
ema.average(update)
不是一个
op
,它只是从
ema
对象的一个字典中取出键对应的
tensor
而已,然后赋值给
ema_val
。这个
tensor
是由一个在
tf.control_dependencies([ema_op])
外部的一个
op
计算得来的,所以
control_dependencies
会失效。解决方法也很简单,看代码:
import tensorflow as tf
w = tf.Variable(1.0)
ema = tf.train.ExponentialMovingAverage(0.9)
update = tf.assign_add(w, 1.0)

ema_op = ema.apply([update])
with tf.control_dependencies([ema_op]):
ema_val = tf.identity(ema.average(update)) #一个identity搞定

with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(3):
print(sess.run([ema_val]))
第二种情况: 这个情况一般不会碰到,这是我在测试
control_dependencies
发现的
import tensorflow as tf
w = tf.Variable(1.0)
ema = tf.train.ExponentialMovingAverage(0.9)
update = tf.assign_add(w, 1.0)

ema_op = ema.apply([update])
with tf.control_dependencies([ema_op]):
w1 = tf.Variable(2.0)
ema_val = ema.average(update)

with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(3):
print(sess.run([ema_val, w1]))
这种情况下,
control_dependencies
也不 work。读取
w1
的值并不会触发
ema_op
, 原因请看代码:
#这段代码出现在Variable类定义文件中第287行,
# 在创建Varible时,tensorflow是移除了dependencies了的
#所以会出现 control 不住的情况
with ops.control_dependencies(None):
...

###################################################################################################
参考这里点击打开链接的信息我们可以知道,TF可以协调多个数据流,在存在依赖的节点下非常有用,例如节点B要读取模型参数值V更新后的值,而节点A负责更新参数V,所以节点B就要等节点A执行完成后再执行,不然读到的就是更新以前的数据。这时候就需要个运算控制器tf.control_dependencies。
参考官方说明文档
format:control_dependencies(self, control_inputs)
arguments:control_inputs: A list of `Operation` or `Tensor` objects which must be executed or computed before running the operations defined in the context. (注意这里control_inputs是list)
return:  A context manager that specifies control dependencies for all operations constructed within the context.(返回所有在环境中的控制依赖的上下文管理器)
其实用法很简单,只有在 control_inputs被执行以后,上下文管理器中的操作才会被执行。例如

[python] view plain copy with tf.control_dependencies([a, b, c]):  
     # `d` and `e` will only run after `a`, `b`, and `c` have executed.  
     d = ...  
     e = ...  
只有[a,b,c]都被执行了才会执行d和e操作,这样就实现了流的控制。当然,官方文档里还介绍了嵌套多个流控制

[python] view plain copy with tf.control_dependencies([a, b]):  
     # Ops constructed here run after `a` and `b`.  
     with tf.control_dependencies([c, d]):  
       # Ops constructed here run after `a`, `b`, `c`, and `d`  
也能通过参数None清除控制依赖例如
[python] view plain copy with g.control_dependencies([a, b]):  
      # Ops constructed here run after `a` and `b`.  
      with g.control_dependencies(None):  
        # Ops constructed here run normally, not waiting for either `a` or `b`.  
        with g.control_dependencies([c, d]):  
          # Ops constructed here run after `c` and `d`, also not waiting  
          # for either `a` or `b`. 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: