例如非严格语义的示例:在下面的示例中,计数器i的最终值不依赖于x。while_loop可并行地增加计数器,并更新x。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-import tensorflow as tf
import tensorflow as tfn = 10
x = tf.constant(0)
c = lambda i, x: i < n
b = lambda i, x: (tf.Print(i + 1, [i],"i:"), tf.Print(x + 1, [x], "x:"))
i, out = tf.while_loop(c, b, (0, x))
with tf.Session() as sess:print(sess.run([i,out]))

i:[0]x:[0]

x:[1]
i:[1]
i:[2]
x:[2]
i:[3]
x:[3]
i:[4]
x:[4]
i:[5]
x:[5]
i:[6]
x:[6]
i:[7]
i:[8]x:[7]

i:[9]
x:[8]
x:[9]
[10, 10]

观察上面例子,x和i是并行计算的,以至于在i为8时,x还是7

i:[6]
x:[6]
i:[7]
i:[8]x:[7]

下面的例子将x设为列表,每次迭代对列表的每个元素加1

#!/usr/bin/env python2
# -*- coding: utf-8 -*-import tensorflow as tf
import tensorflow as tfn = 10
x = tf.constant(list(range(n)))
c = lambda i, x: i < n
b = lambda i, x: (tf.Print(i + 1, [i],"i:"), tf.Print(x + 1, [x], "x:"))
i, out = tf.while_loop(c, b, (0, x))
with tf.Session() as sess:print(sess.run([i,out]))

i:[0]
x:[0 1 2…]i:[1]

x:[1 2 3…]i:[2]

x:[2 3 4…]
i:[3]
x:[3 4 5…]
i:[4]
x:[4 5 6…]
x:[5 6 7…]i:[5]

x:[6 7 8…]i:[6]

x:[7 8 9…]i:[7]

x:[8 9 10…]
i:[8]
i:[9]
x:[9 10 11…]
[10, array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], dtype=int32)]

因为一个循环迭代中的循环计数器依赖于前一个迭代中的值循环计数器本身不能并行递增。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-import tensorflow as tfdef b(i):return tf.Print(i + 2, [i],"i:")def c(i):return tf.less(i,n)n = tf.constant(10)
i = tf.constant(0)res = tf.while_loop(c, b, [i])
with tf.Session() as sess:print sess.run(res)

i:[0]
i:[2]
i:[4]
i:[6]
i:[8]
10
i这个计算器本身,每次迭代增加,因此依赖于前一个迭代中的值。因此,如果我们只想要计数器i的最终值(我们在行打印(sess.run(i)),那么x永远不会递增,但是计数器i将在单个线程上更新。如下所示:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-import tensorflow as tfdef b(i,x):return (tf.Print(i + 2, [i],"i:"),tf.Print(x + 1, [x],"x:"))def c(i,x):return tf.less(i,n)n = 10
i = 0
x = tf.constant(list(range(n)))i,out = tf.while_loop(c, b, (i,x))
with tf.Session() as sess:print sess.run(i)

i:[0]
i:[2]
i:[4]
i:[6]
i:[8]
10
相反,如果我们希望输出值(我们在行打印(sess.run(out))上打印),那么计数器可以在自己的线程上递增,而x可以在单独的线程上并行递增。

注意:因为i每次递增2,所以x只会递增5次,每次增加1

#!/usr/bin/env python2
# -*- coding: utf-8 -*-import tensorflow as tfdef b(i,x):return (tf.Print(i + 2, [i],"i:"),tf.Print(x + 1, [x],"x:"))def c(i,x):return tf.less(i,n)n = 10
i = 0
x = tf.constant(list(range(n)))i,out = tf.while_loop(c, b, (i,x))
with tf.Session() as sess:print sess.run(out)

i:[0]
x:[0 1 2…]i:[2]

x:[1 2 3…]i:[4]

i:[6]x:[2 3 4…]

x:[3 4 5…]
i:[8]
x:[4 5 6…]
[ 5 6 7 8 9 10 11 12 13 14]

在极端情况下,可以想象,递增计数器的线程在x递增一次之前一直运行到完成。唯一不可能发生的事情是线程更新x永远不可能超过计数器线程,因为递增x的线程取决于计数器的值。下面模拟了这种情况(i>6时,x更新递增)

#!/usr/bin/env python2
# -*- coding: utf-8 -*-import tensorflow as tfdef b(i,x):i=tf.Print(i + 1, [i],"i:")x=tf.cond(i<=5,lambda: tf.Print(x,[x],"x:"),lambda: tf.Print(x + 1, [x],"x:"))return (i,x)def c(i,x):return tf.less(i,n)n = 10
i = 0
x = 0i,out = tf.while_loop(c, b, (i,x))
with tf.Session() as sess:print sess.run(out)

i:[0]
x:[0]
i:[1]
x:[0]
i:[2]
x:[0]
i:[3]
x:[0]
i:[4]
i:[5]
i:[6]x:[0]

x:[0]
i:[7]
x:[1]
i:[8]
i:[9]
x:[2]
x:[3]
x:[4]
5

组合tf.cond与tf.while_loop

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""二分法猜数字
"""
import tensorflow as tfdef body(a,b,guessnum,num):center = tf.div(tf.add(a, b),2)a,b,num= tf.cond(guessnum>center, lambda: (center,b,center), lambda: (a,center,center))  return (tf.Print(a,[a],"a:"),tf.Print(b,[b],"b:"),guessnum,num)def c(a,b,guessnum,num):return tf.not_equal(guessnum,num)guessnum = tf.constant(71)
mynum = tf.constant(-1)
a = tf.constant(0)
b = tf.constant(100)a,b,guessnum,num = tf.while_loop(c, body, (a,b,guessnum,mynum))
with tf.Session() as sess:print sess.run(num)

b:[100]
a:[50]
a:[50]
b:[75]
b:[75]
a:[62]
b:[75]
a:[68]
a:[68]
b:[71]
71

tensorflow随笔-条件循环控制(4)相关推荐

  1. tensorflow随笔-条件循环控制(1)

    TensorFlow 提供了一些操作和类,您可以使用它们来控制操作的执行,并向图表添加条件依赖项. tf.identity tf.tuple tf.group tf.no_op tf.count_up ...

  2. tensorflow随笔-条件循环控制(2)

    tf.count_up_to tf.count_up_to( ref, limit, name=None ) 增加"ref"直到它达到"limit" 参数: r ...

  3. tensorflow随笔-条件语句-tf.cond

    tf.cond tf.cond( pred, true_fn=None, false_fn=None, strict=False, name=None, fn1=None, fn2=None ) 如果 ...

  4. tensorflow随笔-条件语句-tf.case

    tf.case tf.case( pred_fn_pairs, default=None, exclusive=False, strict=False, name='case' ) 创建case操作 ...

  5. tensorflow随笔-条件循环语句求解一元多次方程

    !/usr/bin/env python2 -*- coding: utf-8 -*- """ Created on Thu Sep 6 10:16:37 2018 @a ...

  6. TensorFlow实现条件批归一化(Conditional Batch Normalization)

    TensorFlow实现条件批归一化(Conditional Batch Normalization) 条件批归一化(Conditional Batch Normalization) TensorFl ...

  7. tensorflow随笔-tf.while_loop

    tf.while_loop( cond, body, loop_vars, shape_invariants=None, parallel_iterations=10, back_prop=True, ...

  8. tensorflow随笔-底层梯度

    (1)tf.AggregationMethod是一个类 Class AggregationMethod 类拥有的方法主要用于聚集梯度 计算偏导数需要聚集梯度贡献,这个类拥有在计算图中聚集梯度的很多方法 ...

  9. tensorflow随笔-队列管理器QueueRunner-生产者与消费者

    # -*- coding: utf-8 -*- """ Spyder EditorThis is a temporary script file. "" ...

最新文章

  1. axios请求接口http_axios调用接口
  2. python opencv resize函数_Python OpenCV中的resize()函数的使用
  3. 构建安全的Xml Web Service系列之如何察看SoapMessage
  4. STL容器及其简单应用(stack、priority_queue、vector、deuqe、list、map/multimap、set/multiset)
  5. C语言综合期末作业,内蒙古农业大学2010年期末c语言综合作业.doc
  6. 垃圾回收算法与实现系列-JVM无锁实现
  7. matlab 联合体,C++11非受限联合体(union)
  8. 【Elastischearch】Elastischearch 的 ID 生成器 UUIDGenerator
  9. chrome浏览器fitler中的XHR作用是什么
  10. 绝对不能错过!计算机视觉Polygon Mesh Processing读书笔记——4微分几何中的曲线
  11. 【Win 10 应用开发】手写识别
  12. TCP/IP常见英文缩写
  13. java excel转pdf
  14. 数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发
  15. 【R语言数据科学】:(三)数据基础处理(mutate、filter、select等)
  16. 为什么巴西买家更喜欢用Boleto付款?
  17. explorer.exe 应用程序错误 应用程序发生异常 未知软件异常 (0xc0000417),位置为 0x100170e9
  18. Invalid HTTP_HOST header: ‘testserver‘. You may need to add ‘testserver‘ to ALLOWED_HOSTS
  19. flutter 局部状态和全局状态区别_Android 开发者遇到 5G、AI,写给 Android 开发者的 Flutter 指南
  20. cordova 安装ssl证书_android webview https 证书

热门文章

  1. 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________。A.客观地验证需求管理活动...
  2. C# 非模式窗体show()和模式窗体showdialog()的区别
  3. 一个C语言小程序,有10几个命令和MSDOS一样哦:)
  4. SQL2005 用户自定义类型
  5. 雷林鹏分享:jQuery EasyUI 数据网格 - 条件设置行背景颜色
  6. Python 3基础教程32-正则
  7. 关于photoswiper展示时图片自适应的问题
  8. 如何优化页面的响应速度 以及如何减少项目初次加载时间(转https://www.cnblogs.com/MarcoHan/p/5295398.html)...
  9. flume1.8 开发指南学习感悟
  10. 第十七次ScrumMeeting会议