Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ‘RuntimeError: The Session graph is empty. Add operations to the graph before calling run().”

I just simply typed the code given in tf.Tensor Tensorflow 2.0, and here is my code:

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)

But it raised an error:

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

What could I do to fix this error?

like image 238
tieguanyin Avatar asked Jul 25 '19 16:07

tieguanyin


2 Answers

TF 2.0 supports eager execution which means you don't have to explicitly create a session and run the code in it. So the simplest solution would be:

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)

which outputs

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

But you can use the session if you want:

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)

which gives

2.0.0-beta1
[[1. 3.]
 [3. 7.]]
like image 137
John Doe Avatar answered Oct 22 '22 11:10

John Doe


The TensorFlow 2.0 has enabled eager execution by default. At the starting of algorithm, you need to use tf.compat.v1.disable_eager_execution() to 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)

The output gives:

2.1.0
[[1. 3.]
 [3. 7.]]
like image 36
ShadowEspada Avatar answered Oct 22 '22 12:10

ShadowEspada