FailedPreconditionError: Attempting to use uninitialized in Tensorflow
The FailedPreconditionError
arises because the program is attempting to read a variable (named "Variable_1"
) before it has been initialized. In TensorFlow, all variables must be explicitly initialized, by running their "initializer" operations. For convenience, you can run all of the variable initializers in the current session by executing the following statement before your training loop:
tf.initialize_all_variables().run()
Note that this answer assumes that, as in the question, you are using tf.InteractiveSession
, which allows you to run operations without specifying a session. For non-interactive uses, it is more common to use tf.Session
, and initialize as follows:
init_op = tf.initialize_all_variables()sess = tf.Session()sess.run(init_op)
tf.initialize_all_variables()
is deprecated. Instead initialize tensorflow variables with:
tf.global_variables_initializer()
A common example usage is:
with tf.Session() as sess: sess.run(tf.global_variables_initializer())
From official documentation, FailedPreconditionError
This exception is most commonly raised when running an operation that reads a tf.Variable before it has been initialized.
In your case the error even explains what variable was not initialized: Attempting to use uninitialized value Variable_1
. One of the TF tutorials explains a lot about variables, their creation/initialization/saving/loading
Basically to initialize the variable you have 3 options:
- initialize all global variables with
tf.global_variables_initializer()
- initialize variables you care about with
tf.variables_initializer(list_of_vars)
. Notice that you can use this function to mimic global_variable_initializer:tf.variable_initializers(tf.global_variables())
- initialize only one variable with
var_name.initializer
I almost always use the first approach. Remember you should put it inside a session run. So you will get something like this:
with tf.Session() as sess: sess.run(tf.global_variables_initializer())
If your are curious about more information about variables, read this documentation to know how to report_uninitialized_variables
and check is_variable_initialized
.