简体   繁体   English

Tensorflow会话运行不适用于mean_squared_error

[英]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.

相关问题 如何在tensorflow会话中使用mean_squared_error损失 - How to use mean_squared_error loss in tensorflow session 张量流中没有提供渐变(mean_squared_error) - No gradients provided in tensorflow (mean_squared_error) sklean mean_squared_error 忽略平方参数, multioutput='raw_values' - sklean mean_squared_error ignores the squared argument, with multioutput='raw_values' TensorFlow 中的加权均方误差 - Weighted Mean Squared Error in TensorFlow 在这两段代码中计算的 mean_squared_error 有什么区别? 我如何比较 metric='mse' 和 mean_squared_error? - what is the difference between the mean_squared_error computed in both of these pieces of code? how can I compare metric='mse' to mean_squared_error? 由 TensorFlow 计算的均方误差 - Mean squared error computed by TensorFlow TensorFlow 中图像分类模型的均方误差 - Mean Squared Error for image classification model in TensorFlow Tensorflow均方误差损失函数 - Tensorflow mean squared error loss function conv_lstm.py示例使用&#39;binary_crossentropy&#39;损失进行回归。 为什么不使用“ mean_squared_error”呢? - conv_lstm.py example uses 'binary_crossentropy' loss for regression. Why not using 'mean_squared_error' instead? ValueError:维度必须相等,但对于 '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT],维度必须是 68 和 10 - ValueError: Dimensions must be equal, but are 68 and 10 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM