Code:
import tensorflow as tf x = tf.Variable([1, 2]) a = tf.constant([3, 3]) #Add a subtraction op sub = tf.subtract(x, a) #Add an addition op add = tf.add(x, sub) #variables to be initialized init = tf.global_variables_initializer() with tf.Session() as sess: # Initialize variables first sess.run(init) print (sess.run (sub)) print (sess.run (add))
operation result:
[-2 -1] [-1 1]
Code:
#Variables can be named #Create a variable initialized to 0 state = tf.Variable(0, name='counter') #Create an op, the role is to increase the state by 1 new_value = tf.add(state, 1) #Create assignment op, assign new_value to state updata = tf.assign(state, new_value) #variable initialization init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print (sess.run (state)) #loop 5 times for _ in range(5): sess.run(updata) print (sess.run (state))
operation result:
0 1 2 3 4 5