Simple to use TensorFlow in Variable variables

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/nanhuaibeian/article/details/100586339

In TensorFlow, the definition of a string variable, it is the variable, which is different with Python
grammar:state = tf.Variable()

import tensorflow as tf

state = tf.Variable(3,name='count')

# 定义常量 one
one = tf.constant(1)
# 定义加法步骤,但是这步并没有直接计算
new_value = tf.add(state,one)
# 将 State 更新为 new_value
update = tf.assign(state,new_value)

# 如果在 TensorFlow 中定义了变量,那么初始化变量是最重要的
# 定义变量以后,一定要定义 init = tf.initialize_all_variables()
init = tf.initialize_all_variables() # 这步必不可少

with tf.Session() as sess:
    # 变量还没有激活,需要在 sess 里激活 init
    sess.run(init)
    for i in range(3):
        sess.run(update)
        print(sess.run(state))

note:
The last direct print(state)does not work
must take sess a pointer to the state and then print the results in order to get the desired

Guess you like

Origin blog.csdn.net/nanhuaibeian/article/details/100586339