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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010255642/article/details/82423567
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 27 11:16:32 2018
@author: myhaspl
"""
 
import tensorflow as tf
i = tf.constant(0)
c = lambda i: tf.less(i, 10)
b = lambda i: tf.add(i, 1)
r = tf.while_loop(c, b, [i])
sess=tf.Session()
with sess:
    print sess.run(r)

运行结果为10,函数b的最后运行结果。

循环将i每次增加1,直到10

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 27 11:16:32 2018
@author: myhaspl
"""
 
import tensorflow as tf
i = tf.constant(100)
def b(i):
    res=tf.subtract(i, 2)
    return res
    
c = lambda i: tf.greater(i, 0)

r = tf.while_loop(c, b, [i])

sess=tf.Session()
with sess:
    print sess.run(r)

循环每次将i减1,直到0

运行结果为0,函数b的最后运行结果。

猜你喜欢

转载自blog.csdn.net/u010255642/article/details/82423567
今日推荐