求知若渴,大智若愚 Stay Hungry Stay Foolish
1.tensorflow中定义为变量类型,才能成为一个变量,有一点点奇怪2.定义了变量之后,一定要初始化变量才能用
1234567891011121314151617
import tensorflow as tfstate = tf.Variable(0, name = 'counter') #tf.Variable是定义语法# 定义常量,进行一些运算one = tf.constant(1)new_value = tf.add(state, one) #加法update = tf.assign(state, new_value) #更新# 初始化Variableinit = tf.global_variables_initializer() #只要有定义的变量,就一定要执行这个操作# 放入Session,运行就好with tf.Session() as sess: sess.run(init) for _ in range(3): sess.run(update) print(sess.run(state))