tensorflow7 Variable变量

    xiaoxiao2021-09-05  90

    import tensorflow as tf state = tf.Variable(0,name='counter') #print (state.name) one = tf.constant(1) new_value = tf.add(state,one) update = tf.assign(state,new_value) #very important init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for _ in range(3): sess.run(update) print (sess.run(state))

    说明:

    1、tensorflow中变量的定义和C++是一样的,可以有变量的名字和变量的初始值

    2、定义变量之后,都要使用sess.run(init)去进行初始化,这是非常重要的,变量定义完一定要在session里面进行初始化,初始化可以等价为c++中为变量分配空间

    转载请注明原文地址: https://ju.6miu.com/read-677452.html

    最新回复(0)