【问题标题】:Error in tensor evaluation张量评估错误
【发布时间】:2019-01-26 20:22:07
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x tensorflow python-3.5 tensor


    【解决方案1】:

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

    【讨论】:

    • 但是代码在给定链接的环境中运行良好。请看一看。
    【解决方案2】:

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

    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 指出的那样创建一个新的图表。

    【讨论】:

    • 我有python 3.5.2。如何设置与colab环境相同的环境?我的tf版本相同。
    • 看起来这不是版本兼容性问题。请参阅我关于缩进的修订答案。
    【解决方案3】:

    如果您显式创建/使用 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)
    

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2018-05-09
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2018-06-04
      • 2021-04-10
      相关资源
      最近更新 更多