TensorFlow基础3——tf.Variable()和tf.constant()
tf.Variable()生成变量
tf.constant()生成常量
变量需要初始化:
init = tf.initialize_all_variables()
sess.run(init)
1 import tensorflow as tf 2 3 state = tf.Variable(0,name='counter') 4 print(state.name) 5 one = tf.constant(1) 6 7 new_value = tf.add(state,one) 8 update = tf.assign(state,new_value) 9 10 init = tf.initialize_all_variables() 11 12 with tf.Session() as sess: 13 sess.run(init) 14 for step in range(3): 15 sess.run(update) 16 print(sess.run(state))