TensorFlow之variables

TensorFlow的变量处理

import tensorflow as tf
state = tf.Variable(0,name='counter')
print(state.name)
counter:0
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value)
init = tf.global_variables_initializer() #如果定义了变量,一定要先初始化
with tf.Session() as sess:
    sess.run(init)            #初始化必须run之后才生效
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))#所有需要结果的变量都必须run
1
2
3

猜你喜欢

转载自blog.csdn.net/gracejpw/article/details/104109090