piccolo
piccolo

Reputation: 2217

Tensorflow session run not working with mean_squared_error

I am sure there is something obvious that I am overlooking but when I attempt to get the mean squared error with tensorflow I am getting an error message.

import tensorflow as tf

a = tf.constant([3, -0.5, 2, 7])
b = tf.constant([2.5, 0.0, 2, 8])
c = tf.metrics.mean_squared_error(a,b)

sess = tf.Session()
print(sess.run(c))

with the error:

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

But printing c on its own does not yield an error:

print c

(<tf.Tensor 'mean_squared_error/value:0' shape=() dtype=float32>, <tf.Tensor 'mean_squared_error/update_op:0' shape=() dtype=float32>)

Upvotes: 0

Views: 1050

Answers (2)

Patwie
Patwie

Reputation: 4460

According to the implementation the following will work

import tensorflow as tf 
a = tf.constant([3, -0.5, 2, 7])
b = tf.constant([2.5, 0.0, 2, 8])
c = tf.metrics.mean_squared_error(a,b)
sess = tf.InteractiveSession()
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
print(sess.run(c))

Please understand that this is a streaming operation. Do not mix it up with the function tf.losses.mean_squared_error.

Upvotes: 1

Ali Aqrabawi
Ali Aqrabawi

Reputation: 143

you need to initialize variables before accessing them , to initilze :

import tensorflow as tf

a = tf.constant([3, -0.5, 2, 7])
b = tf.constant([2.5, 0.0, 2, 8])
c = tf.metrics.mean_squared_error(a,b)
init = tf.global_variables_initializer() <--
sess = tf.Session()
sess.run(init) <---
print(sess.run(c))

Upvotes: 0

Related Questions