TensorFlow基础学习——tf.control_dependencies()函数用法

转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/83574333
本文出自【我是干勾鱼的博客

Ingredient:

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

TensorFlow中tf.control_dependencies和tf.no_op的意义和用法?

tensorflow随笔-条件循环控制 - tf.no_op(4)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值