1. 程式人生 > >tensorflow學習:定義變數

tensorflow學習:定義變數

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)