Tensorflow会话运行不适用于mean_squared_error

时间:2018-06-23 15:30:40

标签: python tensorflow

我确定有一个明显的东西我正在忽略,但是当我尝试使用张量流获取均方误差时,我会收到一条错误消息。

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

出现错误:

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

但是单独打印c不会产生错误:

print c

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

2 个答案:

答案 0 :(得分:1)

根据the implementation,以下内容将起作用

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

请理解,这是流操作。请勿将其与函数tf.losses.mean_squared_error混淆。

答案 1 :(得分:0)

您需要在访问变量之前初始化变量, 初始化:

connectedCallback()