tensorflow学习之(三)Varible 变量的使用

#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))
原文地址:https://www.cnblogs.com/Harriett-Lin/p/9590205.html