R.K
R.K

Reputation: 67

Why tf. Variable's value does not change upon different calls in the same programe?

In the following code Why tf.Variable valur remains same when I print it twice. Since tf.truncate_normal produces random values so I expect that it should be different upon different calls?

`initial = tf.truncated_normal([2,3], mean=100.0, stddev = 10.0)
 output = tf.Variable(initial)
 sess = tf.InteractiveSession()
 sess.run(tf.initialize_all_variables())
 print output.eval()
 print output.eval()`

Upvotes: 0

Views: 188

Answers (1)

mazecreator
mazecreator

Reputation: 555

The tf.Variable() Op is using the "initial" variable as its initial value. If you look at the help for Variable, you will see the first parameter in the _ init _ method is "initial_value".

Your code calls "tf.initialize_all_variables()" only once which calls the initilaize op the "tf.truncated_normal" which creates the [2,3] matrix to initialize output to the same value. Your code then prints 2 copies of that variable. If you would like to re-init the variable, you need to explicitly state that like this:

initial = tf.truncated_normal([2,3], mean=100.0, stddev = 10.0)
output = tf.Variable(initial)
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
print output.eval()
sess.run(tf.initialize_all_variables())
print output.eval()

This might not be the functionality you are looking for as this has the side effect or re-initializing all variables (trainging weights, etc).

If you are looking to get just the random data set, call the initial op directly. Also note, since you don't have any variables or other Ops that require initialization you don't need that Op to execute to prepare the graph.

initial = tf.truncated_normal([2,3], mean=100.0, stddev = 10.0)
sess = tf.InteractiveSession()
print initial.eval() 
print initial.eval()

You can directly mix the "initial" op with math operators as well. So if you are looking for random variable at each sess.run() don't use a variable but use the initial Op directly.

Upvotes: 1

Related Questions