张量计算中的错误



下面的代码在教程中运行良好,但是当我在本地运行时会出现错误。是否有任何安装错误或其他错误?请帮忙。这是该教程的链接:
https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=tfprogconcepts-colab&hl=en#scrollTo=Md8ze8e9geMi 和代码:

import tensorflow as tf
#create a graph 
g = tf.Graph()
#establish the graph as the default graph 
with g.as_default():
x = tf.constant(8, name = "x_const")
y = tf.constant(5, name = "y_const")
my_sum = tf.add(x,y, name = "x_y_sum")
#create the session
#this runs the default graph
with tf.Session() as sess:
print(my_sum.eval())

下面是发生的错误:

gunjan@gunjan-Inspiron-3558:~/Desktop$ python tf1.py 
/home/gunjan/anaconda3/lib/python3.5/site- 
packages/h5py/__init__.py:34: FutureWarning: Conversion of the second 
argument of issubdtype from `float` to `np.floating` is deprecated. In 
future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
2018-08-20 22:10:41.619062: I 
tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports 
instructions that this TensorFlow binary was not compiled to use: AVX2 
FMA
Traceback (most recent call last):
File "tf1.py", line 15, in <module>
print(my_sum.eval())
File "/home/gunjan/anaconda3/lib/python3.5/site- 
packages/tensorflow/python/framework/ops.py", line 680, in eval
return _eval_using_default_session(self, feed_dict, self.graph, 
session)
File "/home/gunjan/anaconda3/lib/python3.5/site- 
packages/tensorflow/python/framework/ops.py", line 4942, in 
_eval_using_default_session
raise ValueError("Cannot use the default session to evaluate tensor: "
ValueError: Cannot use the default session to evaluate tensor: the 
tensor's graph is different from the session's graph. Pass an explicit 
session to `eval(session=sess)`.

问题是你已经创建了一个图(g(,并且你正在一个单独的图(sess(中执行代码。如果你不需要两个图表,你可以只使用sess

x = tf.constant(8, name = "x_const")
y = tf.constant(5, name = "y_const")
my_sum = tf.add(x,y, name = "x_y_sum")
#create the session
#this runs the default graph
with tf.Session() as sess:
print(my_sum.eval())

要简单地让它工作,您可以显式传递会话,如错误消息所示:

print(my_sum.eval(session=sess))

要了解为什么它不能完全按照教程的规定工作,你可以首先将Python和TensorFlow的版本与教程中使用的版本进行比较。

import tensorflow as tf
import platform
print("Python version: ", platform.python_version())
print("TensorFlow version", tf.__version__)

对于您链接的 colab 环境,这将打印:

Python version:  2.7.14
TensorFlow version 1.10.0

编辑

再看看你的代码示例,这不是版本兼容性的问题。问题是您的教程副本未正确保留缩进。第二个with块需要包含在第一个块中。

# Establish the graph as the "default" graph.
with g.as_default():
# ...
# Now create a session.
# The session will run the default graph.
with tf.Session() as sess:
print(my_sum.eval())

这可确保将g用作会话的默认图形,而不是像 MatthewScarpino 指出错误缩进的版本那样创建一个新图形。

如果显式创建/使用Graph对象而不是使用默认图形,则需要 (a( 将图形对象传递给Session构造函数,或 (b( 在图形上下文中创建会话。

graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session(graph=graph) as sess:
do_stuff_with(sess)

graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session() as sess:
do_stuff_with(sess)

最新更新