Programmers learn & share
0 votes
1.9k views

Problem :

I am new to Python and Tensorflow so I just simply typed the following 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)
# Constructing a `Session` to execute the graph.
sess = tf.compat.v1.Session()
# Executing the graph and storing the value that `e` represents in `result`.
result = sess.run(e)

But it is giving me following error:

2.0.0-beta1
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

How can I fix this error?

by (6.9k points)   | 1.9k views

1 Answer

0 votes

Solution :

The TF 2.0 supports eager execution it means you need not explicitly create the session and run the code in it. So the simplest solution is as follows:

import tensorflow as tf
print(tf.__version__)
# Building 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)

The output as below:

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

But if you want you can use the session as follows:

import tensorflow as tf
print(tf.__version__)
# Constructing 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)
  # Executing the graph and storing the value that `e` represents in `result
  result = sess.run(e)
  print(result)

which gives

2.0.0-beta1
[[1. 3.]
 [3. 7.]]
by (36.1k points)  
2,227 questions
2,734 answers
59 comments
241 users