张量评估错误

Error in tensor evaluation

下面的代码 运行 在教程中没问题,但是当我在本地 运行 时出现错误。是否有任何安装错误或其他问题?请帮忙。这是该教程的 link: 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)`.

要使其正常工作,您可以按照错误消息的建议显式传递会话:

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 指出您的缩进版本不正确那样创建一个新图表。

问题是您已经创建了一个图表 (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())

如果您 create/use 显式 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)