piccolo
piccolo

Reputation: 2227

Tensorflow session not executing function

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)]]

Upvotes: 1

Views: 345

Answers (2)

Vijay Mariappan
Vijay Mariappan

Reputation: 17201

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())

Upvotes: 1

Andreas Storvik Strauman
Andreas Storvik Strauman

Reputation: 1655

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 :)

Upvotes: 1

Related Questions