Solving Tensorflow Error – FailedPreconditionError Attempting to use uninitialized value

FailedPreconditionError Attempting to use uninitialized value is an error which happens in TensorFlow when you try to read a variable before it has been initialized.

In this article I am going to explain why the error FailedPreconditionError Attempting to use uninitialized value is occurring and how to solve it using multiple solutions which may work for your particular case.

Explaining the Error : FailedPreconditionError Attempting to use uninitialized value

The problem happens in TensorFlow when you try to read a variable before it has been initialized.

The error message should look like the error message in the example bellow.

                                                                       #
FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-82-967faab7d494> in <module>()
      4     Target_batch = np.array(OHTarget[i*50:i*50+50].values)
...
    421       raise e_type, e_value, e_traceback
FailedPreconditionError: Attempting to use uninitialized value Variable_1
Caused by op u'gradients/add_grad/Shape_1', defined at:
...
    original_op=self._default_original_op, op_def=op_def)
    self._traceback = _extract_stack()
                                                                       #

The error message is very long, I removed most of it, I hope this is enough for you to see if this error message is close to yours.

Bellow we will describe how the error can be solved. With multiple possible solutions.

Solution 1 : use global_variables_initializer()

The first solution is to use global_variables_initializer() instead of using initialize_all_variables()

As of now, initialize_all_variables() has been deprecated. global_variables_initializer() will help you achieve the same goal.

In your code make sure to replace tf.initialize_all_variables() with tf.global_variables_initializer() without adding anything else.

You can use tf.global_variables_initializer() in you code as follows

                                                                       #
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
                                                                       #

If this worked for you, great. If it did not then try the solution bellow.

Solution 2 : initialize the variable before using it

Another solution is to initialize the variable before using it or them.

You can bulk initialize all variables at the same time by using the code bellow :

                                                                       #
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
                                                                       #

I hope these solutions helped solve your issue.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

This is the end of our post, I hope this has been helpful and helped you solve the error or at least pointed you in the right direction, Learning Python is a fun journey, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/