#coding:utf-8 import tensorflow as tf x = tf.Variable([1,2]) a = tf.constant([3,3]) #增加一个减法op sub = tf.subtract(x,a) #增加一个加法op 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') #创建一个op 作用是使state加1 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 _ in range(5): sess.run(update) print (sess.run(state))