Reputation: 2270
For example, I assume I have something like:
run1 = tf.assign(y,x)
run2 = tf.assign(z,y)
sess.run(run2, feed_dict={x:a}
Will this call first run1, then run2, or do I need to explicitly call run1 first?
I'm having trouble with other code I have. Is this error related at all?
FailedPreconditionError: Attempting to use uninitialized value Variable_11 [[node Variable_11/read (defined at :20) ]]
Upvotes: 0
Views: 333
Reputation: 1186
To answer the title, not always the case, an example where it is true:
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, name="input_vector1")
y = tf.placeholder(dtype=tf.float32, name="input_vector2")
mul1 = tf.multiply(x, y)
mul2 = tf.multiply(mul1, x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(mul2, feed_dict={x: 2, y: 3}))
The operators are all connected, x, and y are inputs provided in the sess.run, and I request mul2, therefore mul1 will first have to be calculated, which will happen automatically in the same sess.run.
Example where it is not the case:
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, name="input_vector1")
y = tf.placeholder(dtype=tf.float32, name="input_vector2")
mul1 = tf.multiply(x, y)
mul2 = tf.multiply(y, x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(mul2, feed_dict={x: 2, y: 3}))
To calculate mul2, there is no need for mul1, so tensorflow wont be bothered with it. To see which operators are connected you can do:
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, name="input_vector1")
y = tf.placeholder(dtype=tf.float32, name="input_vector2")
mul1 = tf.multiply(x, y)
mul2 = tf.multiply(mul1, x)
with tf.Session() as sess:
tf.summary.FileWriter("path/to/empty/folder", sess.graph)
This script will log a logfile to a folder, this logfile can then be read with
tensorboard --logdir=path/to/empty/folder
for more info about tensorboard I refer to the official guide
The error you have is because you did not run sess.run(tf.global_variables_initializer())
, this initializes all variables
Upvotes: 1
Reputation: 145
First of all, you should run below code in order to eliminate the error.
sess.run(tf.global_variables_initializer())
After, you should call run1 and run2 respectively to assign a to z. But if you want to assign a to z with run2, you should define run2 as follow:
run2 = tf.assign(z, run1)
With this code, when you try to call run2, run1 will run and the result of run1 will assign to z.
Upvotes: 1