tensorflow定义变量,并对变量累加
import tensorflow as tf
#给变量赋值为10,并给它起个名字:counter,但貌似没啥用,至少现在还不知道有什么用
stat = tf.Variable(10, name='counter')
one = tf.constant(1)
new_value = tf.add(stat, one)
#stat = new_value
updata = tf.assign(stat, new_value)
#只要之前定义了变量,就需要写这句,否则会有FailedPreconditionError的错误
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(5):
sess.run(updata)
print sess.run(stat)
本文通过一个简单的示例介绍了如何使用TensorFlow定义变量并进行累加操作。首先定义了一个初始值为10的变量,然后创建了一个更新该变量值的操作,最后在会话中运行该操作并打印出结果。
2359

被折叠的 条评论
为什么被折叠?



