Variables

  • Variables must be initialized by running an init Op after having launched the graph. - We first have to add the init Op to the graph.

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))


0
Tensor("Add_7:0", shape=(), dtype=int32)
Tensor("Assign_7:0", shape=(), dtype=int32_ref)
1

Custom Initialization

The convenience function tf.initialize_all_variables() adds an op to initialize all variables in the model. You can also pass it an explicit list of variables to initialize. See the Variables Documentation for more options, including checking if variables are initialized.

Initialization from another Variable

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))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-c33bdd0c2a65> in <module>()
      5 b2 = tf.Variable(biases.initialized_value()*2,name = "b2")
      6 
----> 7 init_op1 = tf.initialize_all_variables([weights])
      8 init_op2 = tf.initialize_all_variables([biases])
      9 init_op3 = tf.initialize_all_variables([w2,b2])

TypeError: initialize_all_variables() takes 0 positional arguments but 1 was given