A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

【转载】        https://blog.csdn.net/u010255642/article/details/82530990

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



  • #!/usr/bin/env python2



  • # -*- coding: utf-8 -*-



  • """



  • Created on Thu Sep  6 10:16:37 2018







  • @author: myhaspl



  • """



  • import tensorflow as tf







  • def b(i):



  •     return tf.Print(i + 2, ,"i:")











  • def c(i):



  •     return tf.less(i,n)







  • n = tf.constant(10)



  • i = tf.constant(0)











  • res = tf.while_loop(c, b, )



  • 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 -*-



  • """



  • Created on Thu Sep  6 10:16:37 2018







  • @author: myhaspl



  • """



  • import tensorflow as tf







  • def b(i,x):



  •     return (tf.Print(i + 2, ,"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 -*-



  • """



  • Created on Thu Sep  6 10:16:37 2018



  • @author: myhaspl



  • """



  • import tensorflow as tf







  • def b(i,x):



  •     return (tf.Print(i + 2, ,"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 -*-



  • """



  • Created on Thu Sep  6 10:16:37 2018



  • @author: myhaspl



  • """



  • import tensorflow as tf







  • def b(i,x):



  •     i=tf.Print(i + 1, ,"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 = 0







  • i,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





2 个回复

倒序浏览

很不错,受教了
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马