tf.Variable()定义变量

import tensorflow as tf
import numpy as np

state = tf.Variable(0)
#print(state.name),输出 "Counter:0"
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init = tf.initialize_all_variables() #定义了变量就一定要这一句

with tf.Session() as sess:
    sess.run(init)    #这一步特别容易忘记,特别容易
    for _in in range(3):
        sess.run(update)
        print(sess.run(state))

结果如下

1
2
3


Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_25974431/article/details/79889111