1

I am using Tensorflow placeholder in order to do a calculation however Tensorflow tells me that I have an initialization error.

import tensorflow as tf
import numpy as np

al = np.array([2.5, 0.0, 2, 8],dtype=float)
bl = np.array([3, -0.5, 2, 7],dtype=float)

sess = tf.Session()

a = tf.placeholder(dtype='float32')
b = tf.placeholder(dtype='float32')


c = tf.metrics.mean_squared_error(a,b)
d = sess.run(c, {a: al, b:bl } )

I cannot see where my mistake is. The error message is:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value mean_squared_error/count
     [[Node: mean_squared_error/count/read = Identity[T=DT_FLOAT, _class=["loc:@mean_squared_error/AssignAdd_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](mean_squared_error/count)]]

2 Answers 2

1

You have to initialise the variables that tf.metrics.mean_squared_error uses:

import tensorflow as tf
import numpy as np

a = tf.placeholder(dtype='float32')
b = tf.placeholder(dtype='float32')
c = tf.metrics.mean_squared_error(a,b)
sess = tf.Session()
# Initialise the local variables
init_local = tf.local_variables_initializer()
sess.run(init_local)
# Initialise the global variables (not really needed here)
# But you'll probably need it in the future when using tensorflow:
init_global = tf.global_variables_initializer()
sess.run(init_global)

al = np.array([2.5, 0.0, 2, 8],dtype=float)
bl = np.array([3, -0.5, 2, 7],dtype=float)
d = sess.run(c, {a: al, b:bl } )

Alternatively, in this case, you can use tf.losses.mean_squared_error.

I would check out tensorflows programmers guide on variables to get a better hold of these things :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But how did you know that tf.metrics.mean_squared_error used local variables? I have also noticed that if I use tf.global_variables_initializer() on its own here I get the same error but if I use tf.local_variables_initializer() on its own the issue is resolved.
@ImranRashid it wouldn't make sense to use variables that could be saved and restored internally in tf.metrics.mean_squared_error, so when it said that a variable inside there (count) was uninitialized then it was a fair chance it was local and not global. (Local variables won't get saved by a saver).
@ImranRashid It also says in the docs: " The mean_squared_error function creates two local variables, total and count that are used to compute the mean squared error"
1

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value mean_squared_error/count

The value count from the above error, is one of the local variable metrics.mean_squared_error uses to calculate MSE (the other is count). So you need to initialize the local variables using:

sess.run(tf.local_variables_initializer())

2 Comments

Thanks Vijay. This worked as well but how did you know to use local_variables_initializer instead of global_variables_initializer?
You dont need global_variables_initializer, the error is regarding local variables, i have added the link in my answer, there you can find the details of the two local variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.