#Varible 变量的使用 使用变量进行自加
import tensorflow as tf

state = tf.Variable(0,name='counter') #定义一个变量,赋值为0,且名字为counter
#print(state.name) 打印结果:counter:0
one = tf.constant(1) # 定义一个常量

new_value = tf.add(state , one)
update = tf.assign(state , new_value) #把new_value的值赋值给state

init = tf.initialize_all_variables() #must have if define varible ,并且使用sess.run来激活 very important

with tf.Session() as sess:
    sess.run(init) #very important
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

 

posted on 2018-09-05 09:32  走丢的蜗牛  阅读(353)  评论(0编辑  收藏  举报