"""
Variable的使用
"""
import tensorflow as tf
state = tf.Variable(0, name='counter') # 一定要定义成是个变量才是个变量,初始值0,名字counter
# print(state.name) # 输出:名字:第几个变量(name:index)
one = tf.constant(1) # 定义一个常量 one = 1
# 变量加常量,还是变量
new_value = tf.add(state, one)
update = tf.assign(state, new_value) # 把new_vlaue的状态加载到state上
init = tf.global_variables_initializer() # 准备初始化所有变量
# 开始运行
with tf.Session() as sess:
sess.run(init) # 初始化所有变量
for _ in range(3):
sess.run(update)
print(sess.run(state))