变量的赋值操做:python
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf ''' TensorFlow中的变量赋值 与传统编程语言不一样,TensorFlow中的变量定义后,通常无需人工赋值, 系统会根据算法模型,训练优化过程当中自动调整变量对应的数值。 特殊状况须要人工更新的,可用变量赋值语句; ''' #变量赋值案例 tf.reset_default_graph() value = tf.Variable(0,name="value") one = tf.constant(1) new_value = tf.add(value,one) update_value = tf.assign(value,new_value) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for _ in range(10): #执行了10次 sess.run(update_value) print(sess.run(value)) logdir = 'F:/python_work/tf/log' writer = tf.summary.FileWriter(logdir,tf.get_default_graph()) writer.close() ''' 输出结果: 1 2 3 4 5 6 7 8 9 10 '''