In [1]:
import tensorflow as tf
In [9]:
# creating a variable : Note we gave an initialization value
state = tf.Variable(0,name = "counter")
one = tf.constant(1)
incr = tf.add(state,one)
# state = state + one produces error
update = tf.assign(state,incr)
# the initialization operation
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
# print(sess.run(state)) ERROR
sess.run(init_op) # initialized my variables
print(sess.run(state))
sess.run(incr)
print(incr)
sess.run(update)
print(update)
print(sess.run(state))
You sometimes need to initialize a variable from the initial value of another variable. As the op added by tf.initialize_all_variables() initializes all variables in parallel you have to be careful when this is needed.
To initialize a new variable from the value of another variable use the other variable's initialized_value() property. You can use the initialized value directly as the initial value for the new variable, or you can use it as any other tensor to compute a value for the new variable.
In [10]:
weights = tf.Variable(tf.random_normal(shape = (3,3),mean = 0,stddev = 1.0),name = "weights")
biases = tf.Variable(tf.random_uniform(shape = (3,1),minval = -1,maxval = 1),name = "biases")
w2 = tf.Variable(weights.initialized_value(),name = "w2")
b2 = tf.Variable(biases.initialized_value()*2,name = "b2")
# init_op1 = tf.initialize_all_variables([weights])
# init_op2 = tf.initialize_all_variables([biases]) DIDN'T WORK
# init_op3 = tf.initialize_all_variables([w2,b2])
init = tf.initialize_all_variables()
with tf.Session() as sess:
# sess.run(init_op1)
# sess.run(inti_op2)
# sess.run(init_op3)
sess.run(init)
print(sess.run(weights))
print(sess.run(biases))
print(sess.run(w2))
print(sess.run(b2))