[英]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: 但是单独打印c不会产生错误:
print c
(<tf.Tensor 'mean_squared_error/value:0' shape=() dtype=float32>, <tf.Tensor 'mean_squared_error/update_op:0' shape=() dtype=float32>)
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 .
不要将其与函数tf.losses.mean_squared_error混淆 。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.