tf.identity 和 tf.control_dependencies的用法

关于 tf.control_dependencies(具体参考博客,也是本文主要参考对象):
tf.control_dependencies(control_inputs)设计是用来控制计算流图的,给图中的某些计算指定顺序。比如:我们想要获取参数更新后的值,那么我们可以这么组织我们的代码。

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

总结一句话就是,在执行某些op,tensor之前,某些op,tensor得首先被运行。


关于tf.identity的用法:

tf.identity(input,name=None)
#Return a tensor with the same shape and contents as input.
#返回一个tensor,contents和shape都和input的一样。

简单地说就是返回了一个一模一样新的tensor,再control_dependencies的作用块下,需要增加一个新节点到gragh中。(别人的总结:为cpu gpu传输什么的提供更好的性能。就像你做一个电路板,有些地方要把线路引出来,调试的时候可以看中间结果一样,tf.identity就是为了在图上显示这个值而创建的虚拟节点。)
在Stack Overflow中有一个问题对tf.identity进行了举例,具体如下:

x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1) # 对x进行加1,x_plus_l是个op

with tf.control_dependencies([x_plus_1]):
    y = x
init = tf.global_variables_initializer()

with tf.Session() as session:
    init.run() # 相当于session.run(init)
    for i in xrange(5):
        print(y.eval()) # y.eval()这个相当于session.run(y)

上面的代码返回结果为:

0.0
0.0
0.0
0.0
0.0

因为这样相当于x_plus_1 这个op没有被运行,因为一般我们在session中会这么执行:

with tf.Session() as session:
    init.run()
    for i in range(5):
        session.run(x_plus_1) #添加了这行代码
        print(y.eval())

返回的结果即是我们想要得到的效果:

1.0
2.0
3.0
4.0
5.0

但是通过tf.identity也可以得到相同的结果:

x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)

with tf.control_dependencies([x_plus_1]):
    y = tf.identity(x)
init = tf.initialize_all_variables()

with tf.Session() as session:
    init.run()
    for i in xrange(5):
        print(y.eval())

下面说明两种 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]))

第二种情况:这个情况一般不会碰到

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):
    ...      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值