Tensorflow变量

import tensorflow as tf

x = tf.Variable([1,2])
a = tf.constant([3,3])
#增加一个减法op
sub = tf.subtract(x,a)
add = tf.add(x,sub)

#初始化所有变量
init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)
print(sess.run(sub))
print(sess.run(add))

#创建一个变量,初始化为0
state = tf.Variable(0,name="counter")
#state加一
new_value = tf.add(state,1)
#把new_value的值赋值给state
update = tf.assign(state,new_value)
#初始化
init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)#调用初始化变量
print(sess.run(state))
for n in range(5):
sess.run(update)
print(sess.run(state))

 

 

 

========================运行结果==========

0
1
2
3
4
5
posted @ 2018-08-10 20:47  西北逍遥  阅读(140)  评论(0编辑  收藏  举报