标签:ESS variables glob var creat env body += ble
计算累加
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 08:25:41 2017
求1+...+5
@author: myhaspl@myhaspl.com
"""
import tensorflow as tf
i = tf.constant(0)
num_sum=tf.Variable(0)
def cond(i,num_sum):
? ? return i <=5
def body(i,num_sum):
? ? num_sum+=i
? ? i+=1
? ? return (i,num_sum)
i,num_sum= tf.while_loop(cond, body, [i,num_sum])
with tf.Session() as sess:
? ? tf.global_variables_initializer().run()
? ? res = sess.run([i,num_sum])
? ? print res
? ? print res[1]
? ? print 1+2+3+4+5
tensorflow的循环
执行结果
[6, 15]
15
15
标签:ESS variables glob var creat env body += ble
原文地址:http://blog.51cto.com/13959448/2339020