【问题标题】:Tensorflow: The Session graph is empty. PythonTensorflow:会话图是空的。 Python
【发布时间】:2020-02-10 11:43:52
【问题描述】:

大家好,我正在使用 Tensorflow 2.0

在这些代码行中:

import tensorflow as tf
hello = tf.constant('Hello World')
sess = tf.compat.v1.Session()
sess.run(hello) <-- Error in this line

RuntimeError:会话图为空。向图中添加操作 在调用 run() 之前。

你知道如何解决吗?

【问题讨论】:

    标签: python tensorflow session tensorflow2.0


    【解决方案1】:

    好吧,伙计们,我找到了路:

    g = tf.Graph() 
    with g.as_default():   
      # Define operations and tensors in `g`.   
      hello = tf.constant('hello')   
      assert hello.graph is g
    
    sess = tf.compat.v1.Session(graph=g) 
    sess.run(hello)
    

    b'你好'

    感谢您的宝贵时间!

    【讨论】:

    • Tensorflow core r2.0 默认启用了 Eager Execution。所以,不用改变它,我们只需要改变我们的代码,如下所示
    • with tf.compat.v1.Session() as sess: # 构建图 hello = tf.constant("hello") print(sess.run(hello))
    【解决方案2】:

    Tensorflow core r2.0 默认启用了 Eager Execution。因此,无需更改它,我们只需通过在会话中启动图表来更改我们的代码,如下所示。

    > with tf.compat.v1.Session() as sess:
    >     # Building a graph
    >     hello = tf.constant("hello")
    >     print(sess.run(hello))
    

    根据 Tensorflow 文档..

    一个默认的 Graph 总是被注册的,并且可以通过调用来访问 tf.compat.v1.get_default_graph

    对于不需要声明 tf.Graph() 的基本操作,您可以定义一个具有更多计算和数据集的图,您可以定义一个图并调用到会话中。

    请参考:更多信息 https://www.tensorflow.org/api_docs/python/tf/Graph https://github.com/OlafenwaMoses/ImageAI/issues/400

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2020-01-04
      • 1970-01-01
      • 2017-07-24
      • 2017-11-02
      • 2019-01-07
      • 1970-01-01
      相关资源
      最近更新 更多