【问题标题】:How to fix ‘RuntimeError: The Session graph is empty. Add operations to the graph before calling run().”如何修复'RuntimeError:会话图为空。在调用 run() 之前向图中添加操作。”
【发布时间】:2023-03-21 03:16:01
【问题描述】:

我只是简单地输入了tf.Tensor Tensorflow 2.0 中给出的代码,这是我的代码:

import tensorflow as tf
print(tf.__version__)
# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)

# Construct a `Session` to execute the graph.
sess = tf.compat.v1.Session()

# Execute the graph and store the value that `e` represents in `result`.
result = sess.run(e)

但它引发了错误:

2.0.0-beta1
2019-07-25 17:06:35.972372: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "/Users/yupng/Documents/Dissertation/kmnist/kminst_v1.0.py", line 14, in <module>
    result = sess.run(e)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 950, in run
    run_metadata_ptr)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1098, in _run
    raise RuntimeError('The Session graph is empty.  Add operations to the '
RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

Process finished with exit code 1

我可以做些什么来修复这个错误?

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    TF 2.0 支持即时执行,这意味着您不必显式创建会话并在其中运行代码。所以最简单的解决方案是:

    import tensorflow as tf
    print(tf.__version__)
    
    # Build a dataflow graph.
    c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
    e = tf.matmul(c, d)
    
    print(e)
    

    哪个输出

    2.0.0-beta1
    tf.Tensor(
    [[1. 3.]
     [3. 7.]], shape=(2, 2), dtype=float32)
    

    但你可以根据需要使用会话:

    import tensorflow as tf
    print(tf.__version__)
    
    # Construct a `Session` to execute the graph.
    with tf.compat.v1.Session() as sess:
    
      # Build a dataflow graph.
      c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
      d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
      e = tf.matmul(c, d)
    
      # Execute the graph and store the value that `e` represents in `result`.
      result = sess.run(e)
      print(result)
    

    给了

    2.0.0-beta1
    [[1. 3.]
     [3. 7.]]
    

    【讨论】:

    • 如果回答解决了您的问题,请采纳。
    • 请注意:为了使它们真正等效,第一个示例应该是 print(e.numpy())
    【解决方案2】:

    TensorFlow 2.0 默认启用了 Eager Execution。在算法开始时,您需要使用tf.compat.v1.disable_eager_execution() 来禁用急切执行。

    import tensorflow as tf
    
    tf.compat.v1.disable_eager_execution()
    
    print(tf.__version__)
    
    # Build a dataflow graph.
    c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
    e = tf.matmul(c, d)
    
    # Construct a `Session` to execute the graph.
    sess = tf.compat.v1.Session()
    
    # Execute the graph and store the value that `e` represents in `result`.
    result = sess.run(e)
    print(result)
    

    输出给出:

    2.1.0
    [[1. 3.]
     [3. 7.]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多